Check if ID is equal

I need to remove a class from all table cells when the parent of the element has a specific identifier.

$(".closethis").click(function(){ var $this = $(this).parent().parent(); if ($this.attr("id") == "mainArea") { $("#myTbl").removeClass("myClass"); } }); 

I need to check the identifier because it is part of the function, and if the ID is not equal to this value, this is probably for another case.

It looks right, but it doesn't seem to work. Am I missing something?

+6
source share
2 answers

Hmm .. Ok .. $ var looks like PHP .. I would just call it. In addition, you have #. in your myTbl, which means id and class .... so it depends on how you identify the cells .. but assuming the cells are td inside the ID'd table "myTbl", try this

 $(".closethis").click(function(){ var checkDiv = $(this).parent().parent(); if (checkDiv.attr("id") == "mainArea") { $("#myTbl").find('td').removeClass("myClass"); } }); 

If you cannot complete this work, give us the identifier or class of each element, and we will be able to provide you with the correct code.

+8
source

Try using the is function present in jQuery.

 if ($this.is('#mainArea')) { ... } 
+9
source

Source: https://habr.com/ru/post/910989/


All Articles