Karem
Karem

Reputation: 18103

Dont hide if it hasClass in jQuery

Okay, start out saying this is hard to explain with words only.

So i have setup a example here: http://jsfiddle.net/AFhvS/2/

I have a code in PHP, that show() the span element with X .sletindtastningfelt if it's filled.

In the jsfiddle, it's placed at the bottom in the js field.

jQuery(document).ready(function() {
    $('#smeal-1-1').show();
    $('#smeal-1-1').addClass("upstartRemoveClass");

    $('#smeal-1-2').show();
    $('#smeal-1-2').addClass("upstartRemoveClass");
}); 

Ok so it appears X infront of the first two input fields now.

What it does now is that when you click on one of the 4 tr (input fields), it hides the other X's and keeps the one infront of the field. This is caused by the .end().siblings().find('.sletindtastningfelt').hide();

I wish to change this procedure if it has the class "upstartRemoveClass". So if it has the class upstartRemoveClass it should not hide, like the rest, when you click on a tr (input field)

Upvotes: 0

Views: 2051

Answers (4)

Félix Saparelli
Félix Saparelli

Reputation: 8719

The answers above using .not() or :not() are valid, but you can also change the logic a bit, and show any .upstartRemoveClass after hiding the rest:

$(this).closest('tr')
       .find('.sletindtastningfelt')
       .show()
       .end()
       .siblings()
       .find('.sletindtastningfelt')
       .hide();

$('.upstartRemoveClass').show();

In this case, it will probably not be the best choice, but it can be useful sometimes, for example to remove complexity.

Upvotes: 0

Deleteman
Deleteman

Reputation: 8680

How about using not?

Like this:

.end().siblings().find('.sletindtastningfelt').not(".upstartRemoveClass").hide();

Does that give you the result you're looking for?

Upvotes: 1

voigtan
voigtan

Reputation: 9031

Maybe you can use :not(.className):

.end().siblings().find('.sletindtastningfelt:not(.upstartRemoveClass)').hide();

Upvotes: 2

JNDPNT
JNDPNT

Reputation: 7465

This just requires a condition:

if($("someElement").hasClass("upstartRemoveClass"))
{
   // do something
}

Upvotes: 1

Related Questions