How to properly use the .each() command in jQuery
Posted
by sadmicrowave
on Stack Overflow
See other posts from Stack Overflow
or by sadmicrowave
Published on 2010-04-26T12:51:32Z
Indexed on
2010/04/26
12:53 UTC
Read the original article
Hit count: 235
I have a script that checks the class (integer) of a , runs a switch statement to change that integer value to text and appends the text to another in the same listitem tag. I use the .each() function because each listitem starts with class=_[user ID] -- each user can have up to 5 entries.. Enough explaining -- heres the code:
<HTML>
<li class='_44074'><div class='_12' style='width:380px;'><div style='width:60px; float:left;'>1st</div><div class='verify-type' style='float:left; width:160px;'></div><div style='float:left; width:120px;'>04/26/10 07:29 AM</div></div></li>
<li class='_44074'><div class='_6' style='width:380px;'><div style='width:60px; float:left;'>2nd</div><div class='verify-type' style='float:left; width:160px;'></div><div style='float:left; width:120px;'>04/23/10 03:29 PM</div></div></li>
<li class='_44074'><div class='_12' style='width:380px;'><div style='width:60px; float:left;'>3rd</div><div class='verify-type' style='float:left; width:160px;'></div><div style='float:left; width:120px;'>04/23/10 03:18 PM</div></div></li>
<li class='_44074'><div class='_2' style='width:380px;'><div style='width:60px; float:left;'>4th</div><div class='verify-type' style='float:left; width:160px;'></div><div style='float:left; width:120px;'>04/23/10 02:28 PM</div></div></li>
</HTML>
when I use the .each() function to scan through each of the listitems begining with the entered user id it only finds the first value (in this case _12) and applies that to all the entries; instead of finding _12, _6, _12, _2 it finds _12, _12, _12, _12...here is the java:
$("div#history-menu div#history-text li." + valueid).each(function(){
valueid = $("div#center-box input").val();
checkedvalue="";
checkedvalue = $("div#history-menu div#history-text li." + valueid + " div").attr('class');
switch(checkedvalue){
case '_2':lcCheckedMessage = "Shoes"; break;
case '_4':lcCheckedMessage = "Shoe Straps"; break;
case '_6':lcCheckedMessage = "Shoes & Shoe Straps"; break;
case '_8':lcCheckedMessage = "Wrist Straps"; break;
case '_10':lcCheckedMessage = "Shoes & Wrist Strap"; break;
case '_12':lcCheckedMessage = "Shoe Straps & Wrist Strap"; break;
};
$("div#history-menu div#history-text li." + valueid + " ." + checkedvalue + " .verify-type").text(lcCheckedMessage);
});
© Stack Overflow or respective owner