Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-20-2011, 11:46 PM   PM User | #1
AmmarIdris88
New Coder

 
Join Date: Nov 2011
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
AmmarIdris88 is an unknown quantity at this point
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.
AmmarIdris88 is offline   Reply With Quote
Old 11-21-2011, 01:36 AM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,592
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
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)')... 
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Users who have thanked VIPStephan for this post:
AmmarIdris88 (11-21-2011)
Old 11-21-2011, 10:22 AM   PM User | #3
AmmarIdris88
New Coder

 
Join Date: Nov 2011
Posts: 21
Thanks: 12
Thanked 0 Times in 0 Posts
AmmarIdris88 is an unknown quantity at this point
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.
AmmarIdris88 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:09 AM.


Advertisement
Log in to turn off these ads.