Find if clicked list item is before currently selected item?

Find if clicked list item is before currently selected item?

I am trying to set a flag if a clicked list item is before the currently selected item in the list. Below is some partial code (the loop from another function altogether!) that shows:
- CSS list ID as "#sub_menu"
- selected class = "on"
- "itm" is the currently clicked on list item

Any help on this? THANKS in advance.


  1. function loadItem (itm) {
  2. // try to find if the clicked item is before the currently hilighted item
  3. // if so, set  var goingBack = true;
  4. var prevClicked;
  5. var nowClicked;
  6. $('#sub_menu li a').each(function(i) {
  7.     if ($(this).attr("id") == itm) {
  8.         $(this).addClass("on").removeClass("off");
  9.     } else {
  10.         $(this).addClass("off").removeClass("on")
  11.     }
  12. });
Pseudo-code would be

  1. $('#sub_menu li a').each(function(i) {
  2.     if this item is highlighted {
  3.         set prevClicked to the index of this item;
  4.     }
  5. set nowClicked = the index of the currently selected item
  6. if nowClicked < prevClicked then goingBack = true
  1. });