1 Replies - 1104 Views - Last Post: 27 April 2012 - 11:17 AM

#1 mlotfi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-February 07

newbie, closest method ?

Posted 22 April 2012 - 12:40 PM

Hi,
I just started learning Jquery, I found this tutorial :

http://www.tutorials...sal-closest.htm

it said :

Description:
The closest( selector ) method works by first looking at the current element to see if it matches the specified expression, if so it just returns the element itself. If it doesn't match then it will continue to traverse up the document, parent by parent, until an element is found that matches the specified expression. If no matching element is found then none will be returned.

<html>
<head>
<title>the title</title>
   <script type="text/javascript" 
   src="/jquery/jquery-1.3.2.min.js"></script>
 <script>
  $(document).ready(function(){

    $(document).bind("click", function (e) {
      $(e.target).closest("li").toggleClass("highlight");
    });

  });
  </script>
  <style>
  .highlight { color:red; 
          background: yellow;
        }
  </style>
</head>
<body>
   <div>
   <p>Click any item below to see the result:</p>
   <ul>
     <li class="top">list item 1</li>
     <li class="top">list item 2</li>
     <li class="middle">list item 3</li>
     <li class="middle">list item 4</li>
     <li class="bottom">list item 5</li>
     <li class="bottom">list item 6</li>
   </ul>
   </div>
</body>
</html>



when you click in a list item it change its class.
what I did now, changed :
<script>
  $(document).ready(function(){
    $(document).bind("click", function (e) {
      $(e.target).closest("div").toggleClass("highlight");
    });
  });
  </script>



AND

<body>
   <div class="someclass">
   <p>Click any item below to see the result:</p>
   <ul>
     <li class="top">list item 1</li>
     <li class="top">list item 2</li>
     <li class="middle">list item 3</li>
     <li class="middle">list item 4</li>
     <li class="bottom">list item 5</li>
     <li class="bottom">list item 6</li>
   </ul>
   </div>
</body>



since div is one of the li parent, so clicking in any li should change the class of div to yellow, but it does not work, maybe I misunderstood how this function works or I am missing something.

your help is appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: newbie, closest method ?

#2 maxfarnham   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 5
  • Joined: 27-April 12

Re: newbie, closest method ?

Posted 27 April 2012 - 11:17 AM

That's overcomplicating it. Rather than bind to the document like you do here:
$(document).bind("click", function (e) {
      $(e.target).closest("li").toggleClass("highlight");
    });


Instead, bind the event to the list items and use the this selector like so:
$("li").bind("click", function () {
      $(this).toggleClass("highlight");
    });

The $("li").bind binds the event to all the <li>'s on the page, so each one clicked will fire the event independently.The this selector is very powerful - if you plan on doing a lot of jquery you will be seeing quite a bit of it. It selects the <li> that fired the event, so only that one gets the function executed on it.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1