Cloudboy22
Cloudboy22

Reputation: 1514

Selecting other elements with a same class name and not this perticular element

Hey guys I am trying to select images with same class name and exclusing a single image with an id. I am quite confused here. how should I do this Here are my bunch of images

 <li><img id="firstImage" class="fullBg anim" src="img/1.jpg"></li>
  <li><img id="secondImage" class="fullBg anim" src="img/2.jpg"></li>  
  <li><img id="thirdImage" class="fullBg anim" src="img/3.jpg"></li>
  <li><img id="fourthImage" class="fullBg anim" src="img/4.jpg"></li>            
  <li><img id="fifthImage" class="fullBg anim" src="img/5.jpg"></li>
  <li><img id="sixthImage" class="fullBg anim" src="img/6.jpg"></li>
  <li><img id="seventhImage" class="fullBg anim" src="img/7.jpg"></li>
  <li><img id="eigthImage" class="fullBg anim" src="img/8.jpg"></li>

Now If i have to select the #secondImage image and do something to the rest images with a class name fullBg how to do it. please help, thanks.

Upvotes: 0

Views: 50

Answers (3)

Pranav C Balan
Pranav C Balan

Reputation: 115272

You can use not() method in jQuery, to add class use addClass()

Example :

$('img.anim').not('#secondImage').addClass('fullBg');

The above example will select all img tag having class anim except the img tag having id secondImage

Documentation

Upvotes: 1

designerNProgrammer
designerNProgrammer

Reputation: 2701

So bascially the most elegent way of doing this is creating a class and putting all the added stuff to it. And then do this

    $('img.anim').not('#secondImage').addClass('yourClass');

That way the code would be more optimised.

Upvotes: 0

Amit
Amit

Reputation: 15387

Try this

$(function(){
  $(".fullbg:not('#secondImage')").each(function(){
    alert($(this).attr("id"));
   });
});

Upvotes: 1

Related Questions