I have a javascript code as follow:
$( "#hover0" )
.mouseenter(function() {
$( "#hover0" ).attr("style","background-color:#e1e8ed;");
})
.mouseleave(function() {
$( "#hover0" ).removeAttr();
});
which works perfectly but as soon as I change it to the following it does not work:
var item=0;
$( "#hover"+item )
.mouseenter(function() {
$( "#hover"+item ).attr("style","background-color:#e1e8ed;");
})
.mouseleave(function() {
$( "#hover"+item ).removeAttr();
});
what is the problem?Can anyone help me how I can do it like the second approach?(Actually the real scenario is a for loop with item changing as each loop passes)
Update:
Here is my loop:
for (var item in jsresult) {
if (jsresult[item] != "null") {
$('#tweetWrapper')
.append("<div class='tweetCon' id='hover"+item+"' >" +
"<div class='tweetimgcon' ><img alt='' height='50px' src='"+jsresult[item].url+"'></div>" +
"<div class='tweetcontitle' >"+jsresult[item].name+"</div>" +
"<div class='tweetcondate' >"+jsresult[item].date+"</div>" +
"<div class='tweetconcontent' '>"+jsresult[item].text+"</div>" +
"</div><div class='ttest' style='float:left;height:300px;width:100%;display:none;'></div>");
$("#hover0")
.mouseenter(function() {
$( "#hover0" ).attr("style","background-color:#e1e8ed;");
})
.mouseleave(function() {
$( "#hover0" ).removeAttr();
});
}
}