Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if ID equals

Tags:

jquery

I need to remove a class from all table cells when an element's parent has a particular ID.

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

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

Looks right but it does not seem to work. Am I missing something?

like image 968
santa Avatar asked Mar 17 '12 15:03

santa


People also ask

What method s would you use to check if an element has a specific id and if so replace it with a different id?

Approach 2: First, we will use document. getElementById() to get the ID and store the ID into a variable. Then use JSON. stringify() method on the element (variable that store ID) and compare the element with 'null' string and then identify whether the element exists or not.

How to check id in if condition javascript?

If you're trying to check the value of the ID property then you can get it using the attr method. Show activity on this post. // element could be any html element but inputs have val and div, spans and suchlike don't have val var Submit_Status = $('element'). attr('id') == 'Reset' ?


1 Answers

Hmm.. Ok.. $var looks like PHP.. i'd just call it was it is. Also, you have #. in your myTbl which means id and class.... so it depends on how you are identifying the cells.. but assuming the cells are td's inside a table ID'd "myTbl", try this

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

If you can't make that work, give us the ID's or class's of each element and we can give you the correct code.

like image 170
whiteatom Avatar answered Oct 16 '22 20:10

whiteatom