I’m trying to manipulate an id to use the same button to do more then 2 actions.
its something like:
var id;
if(something == 2){
id = "id";
}else{
id = "id2";
}
$(".myClass").attr("id", id);
but, its not good, because if i try to use the id that i set, won’t work, maybe because the document was already loaded with no id in the button.
dont think this need a example, but i made it.
have another way to do this ?
If you need to bind actions to some future element, it’s generally best to use delegation, meaning to listen for the events not on the elements themselves, but on one of their ancestors:
At this point, you can set the ID dynamically:
While I am listening for the click event on the body, it’s generally best to use a closer parent element so the event doesn’t have to bubble too much before being handled.