Reputation: 41
I am trying to select HTML elements by ID in order to modify their attributes but for some reason it is not working. I have defined my elements as:
When I try to select the elements by doing the following I do not get any effect.
function handleClick(id){
var ID = parseInt(id);
$("#ID").css({"background-color": "white"});
}
However, if I make the call with the following instead I get the desired effect: $("#16").css({"background-color": "white"});
How can I change my code so that I can do the changes based on the id parameter?
Upvotes: 0
Views: 1723
Reputation: 2166
If there is a tag on the page with the id
of 16
the code should look like this:
function handleClick(id){
var ID = parseInt(id);
$("#"+ID.toString()).css({"background-color": "white"});
}
So if 16
is passed to the id
it will run like this:
function handleClick("16"){
var ID = parseInt("16"); //int id
$("#"+16.toString()).css({"background-color": "white"});
//will not work because raw ints do not support toSting(). Jquery will look for "#16"
}
Upvotes: 1