Jitender
Jitender

Reputation: 7971

Hide div with match ID and Class Using jquery

I want hide div which has id 'hide' and class 'test'. It is working fine with .filter method but I want to is there another method to do this. fiddle

if($('#hide').hasClass('test')){
   $('.test').hide();
}

or

$('#hide').filter(function(){
   return this.className=='test'
}).hide();

Upvotes: 0

Views: 287

Answers (1)

PSL
PSL

Reputation: 123739

You can write it as

$('#hide.test').hide();

no space between selectors will select the element which has id hide and class test

Not quite clear on what you meant by dynamic but as @kolink said you can just concatinate them. If there are many classnames in an array you could even do.

var toHide = ["hide", "if", "allarepresent"];
$("#hide." + toHide.join('.')).hide();

Fiddle

Upvotes: 6

Related Questions