I'm passing an array idArray to a jQuery each function, which I'm then using to get the values. I'm trying to use the values to create a new jQuery selector.
Here's what an example idArray looks like:
Array[4]
0 : "5"
1 : "6"
2 : "8"
3 : "9"
Here's my code:
function editGigs(idArray) {
console.log(idArray); // Produces array correctly
$(idArray).each(function(k, v) {
trId = "'#row" + v + "'";
console.log(trId); // Produces '#row5'
$(trId).find('.td.forename').css('background-color', 'black');
});
}
I'm getting an error, as follows:
Uncaught Error: Syntax error, unrecognized expression: '#row5'
However, when I add '#row5' into the last line of the code itself as the id selector, it works...?! Something appears to be wrong in the way I'm using a value from the jQuery each function as the id selector.
trId = "#row" + v;$(idArray)isn't a great idea as jQuery is expecting an array of DOMElements, not strings. You could useidArray.forEach(), or a simpleforloop instead'#row5'(with the quotes) is not a valid selector