Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If any list item is clicked first, do this, else do this

Tags:

jquery

click

So I have an unordered list of links, and I need to say "If any of these links are clicked for the first time do this, else do this" and I don't mean any individual link in the list, if just one of these items is clicked, that is the first click for all of them.

$('.idTabs li a').one("click", function() {
    do this;
});

This works, but only for each individual link in the list. It will work on click of list-item-1 and not appear again, if I click list-item-2 it will do it once and not appear again. What I need is to click on list-item-1 and have it not appear again for any other list-item-#.

Further background to better understand: I have a list navigation that expands a hidden sub-navigation when you click on one item, after clicking that first item, the sub-navigation appears, and after it appears—clicking on any other parent item opens it's corresponding sub-navigation like tabs.

like image 596
RooWM Avatar asked Dec 07 '25 02:12

RooWM


1 Answers

You could use .data() to store a boolean value, associated with the unordered list, indicating if it is the first click or not:

$('.idTabs li a').on("click", function() {
  var $idTabs = $(".idTabs");
  var clicked = $idTabs.data("clicked");
  if (!clicked) { // first click
    $idTabs.data("clicked", true);
    console.log('First click');
  } else {
    console.log('Not the first click');
  }
});​

DEMO.

like image 173
João Silva Avatar answered Dec 08 '25 16:12

João Silva