Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Selecting elements which haven't been clicked

    Hi,

    What would be the best method to select the elements in a container which haven't been clicked when one of the elements in that container has been clicked? That won't make any sense without an example so lets say we have a div which contains several other divs, like so:
    Code:
    <div id="container">
    
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
      <div class="item"></div>
    
    </div>
    ... and I use jQuery to do something when an item is clicked, how do I refer to all the other items which haven't been clicked? Something like .each() except for the item which was just clicked. I'm not looking for any sort of long method to do this, if that is what's necessary I can write it myself. I just want to make sure that I haven't overlooked an existing jQuery method that can do it really simply.

    Thank you.

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,680
    Thanks
    6
    Thanked 1,009 Times in 982 Posts
    Assign a class to each item on click and then address all elements that don’t have that class. E. g.:
    PHP Code:
    $('#container > div').click(function() {
      $(
    this).addClass('clicked');
    });

    ...

    $(
    '#container > div:not(.clicked)')... 

  • Users who have thanked VIPStephan for this post:

    AmmarIdris88 (11-21-2011)

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by VIPStephan View Post
    Assign a class to each item on click and then address all elements that don’t have that class. E. g.:
    PHP Code:
    $('#container > div').click(function() {
      $(
    this).addClass('clicked');
    });

    ...

    $(
    '#container > div:not(.clicked)')... 
    Thanks, a very simple solution. I overlooked the :not selector which is perfect for my problem. Thank you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •