views:

41

answers:

1

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...and applies the switch value "Shoe Straps & Wrist Straps" to all entries -- 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);
});
+3  A: 

This line:

checkedvalue = $("div#history-menu div#history-text li." + valueid + " div").attr('class');

will perform the search again, always finding the first item. Instead, use the value of this to get the current item:

checkedvalue = $(this).find('div').attr('class');

You need to do something similar in the last line, e.g.

$(this).find(".verify-type").text(lcCheckedMessage);
interjay
Holy Crap you are AWESOME!!! thank you so much!! that was perfect -- so freaking easy
sadmicrowave
Using `.find('div')` will return all nested divs. There are several (seen if you scroll to the right). You need to make it specific to the first `div`.
patrick dw
@patrick: True, but `attr(..)` will only get the attribute value for the first element, so it doesn't matter if more than one `div` matched.
interjay
@interjay: OK, good point. Still the obsessive compulsive part of me tells me that selectors ought be specific to the element(s) you actually want. But you're absolutely right. Only the `attr` is being returned. Not the collection. +1
patrick dw