Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, March 22, 2016

jQuery: select all elements of a given class, except for a particular Id

jQuery: select all elements of a given class, except for a particular Id


This is probably pretty simple.

I want to select all elements of a given class thisClass, except where the id is thisId.

i.e. something equivalent to (where -/minus implies remove):

$(".thisClass"-"#thisId").doAction();  

Answer by rahul for jQuery: select all elements of a given class, except for a particular Id


Use the :not selector.

$(".thisclass:not(#thisid)").doAction();  

Answer by Amy B for jQuery: select all elements of a given class, except for a particular Id


$(".thisClass[id!='thisId']").doAction();

Documentation on selectors: http://api.jquery.com/category/selectors/

Answer by abimelex for jQuery: select all elements of a given class, except for a particular Id


Or take the .not() method

https://api.jquery.com/not/

$(".thisClass").not("#thisId").doAction();  

Answer by NiZa for jQuery: select all elements of a given class, except for a particular Id


Using the .not() method with selecting an entire element is also an option.

This way could be usefull if you want to do another action with that element directly.

$(".thisClass").not($("#thisId")[0].doAnotherAction()).doAction();  

Answer by ScottyG for jQuery: select all elements of a given class, except for a particular Id


You could use the .not function like the following examples to remove items that have an exact id, id containing a specific word, id starting with a word, etc... see http://www.w3schools.com/jquery/jquery_ref_selectors.asp for more information on jQuery selectors.

Exact ID:

 $(".thisClass").not('[id="thisId"]').doAction();  

ID contains the word "Id"

$(".thisClass").not('[id*="Id"]').doAction();  

ID starts with "my"

$(".thisClass").not('[id^="my"]').doAction();  

Answer by Jayant Bhawal for jQuery: select all elements of a given class, except for a particular Id


I'll just throw in a JS (ES6) answer, in case someone is looking for it:

Array.from(document.querySelectorAll(".myClass:not(#myId)")).forEach((el,i) => {      doSomething(el);  }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.