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.
- function loadItem (itm) {
- // try to find if the clicked item is before the currently hilighted item
- // if so, set var goingBack = true;
- var prevClicked;
- var nowClicked;
- $('#sub_menu li a').each(function(i) {
- if ($(this).attr("id") == itm) {
- $(this).addClass("on").removeClass("off");
- } else {
- $(this).addClass("off").removeClass("on")
- }
- });
Pseudo-code would be
- $('#sub_menu li a').each(function(i) {
- if this item is highlighted {
- set prevClicked to the index of this item;
- }
- set nowClicked = the index of the currently selected item
- if nowClicked < prevClicked then goingBack = true
- });