var left=10;
var right=50;
$("#leftori").attr("id",left);
$("#rightori").attr("id",right);//change the id
$(document).delegate('div[id^='+left+']', 'mouseenter',function() {
alert("left ok!");
var newleft=left+1;
$("#"+left).attr("id","newleft"};//change the id
left=newleft;
}
$(document).delegate('div[id^='+right+']', 'mouseenter',function() {
alert("right ok!");
var newright=right+1;
$("#"+right).attr("id","newright");
right=right+1;
}
<body>
<div id="leftori" class="div01"></div>
<div id="rightori" class="div01"></div>
</body>
The Questions are:
- How can I delegate the dynamical “id” on the new div which changed the id attribute on the second mouseenter event?
-
I want to combine the selector with the
id^=leftorid^=rightin the same delegate function . Can any method to make it? For example:$(document).delegate('div[id^='+left+']' || 'div[id^='+right+']' , 'mouseenter',function() {...}
But it cannot work well…How can I fix it or not have other method but to write in two delegate functions?
I would suggest that you don’t change the ID of any DOM element – there are far better ways to keep “data” on a DOM element ….
How about something like this :
HTML :
JavaScript :
Incase you are not aware – the
console.logcommand logs the output to the browsers javascript console of a debugger (firebug).docs
Working example : http://jsfiddle.net/8RhbB/1/