Jessica
Jessica

Reputation: 3769

What can of selector do I need to select for the presence of an ID and a class?

I would like to check for

#dialogOrder

and

.menu-mask

Is the following valid and is there an even easier shortcut ?

$("#dialogOrder").$(".menu-mask")

Update

I just realized I also need to select the element only if it is on a form with the class of menu. Sorry that this was not part of the original question. I hope someone can help me.

Upvotes: 1

Views: 76

Answers (4)

Phil
Phil

Reputation: 164723

If you mean you want to find an element with both ID dialogOrder and class menu-mask, simply use

jQuery("#dialogOrder.menu-mask")

If you want to know if this element exists in your document, try

if (jQuery("#dialogOrder.menu-mask").length > 0) {
    // element exists
}

Of course, adding the class selector is redundant as there should only be at most one element with any given ID attribute.

Update

To answer your comment, if you mean you want to find dialogOrder in

<form class="menu">
    <div id="dialogOrder">...</div>
</form>

You can use several methods

$("form.menu").find("#dialogOrder")

or

$("form.menu #dialogOrder")

Upvotes: 2

zzzzBov
zzzzBov

Reputation: 179046

You chain the selectors:

$('#dialogOrder.menu-mask')

If you need to use a pre-existing selector, you can use the filter method:

$('.menu-mask').doStuff().filter('#dialogOrder').doMoreStuff();

Upvotes: 1

seeming.amusing
seeming.amusing

Reputation: 1179

If you are looking for a tag that is both #dialogOrder AND .menu-mask, then you should use:

$("#dialogOrder.menu-mask")

Upvotes: 3

jwchang
jwchang

Reputation: 10864

Try below

$("#dialogOrder.menu-mask");

Upvotes: 1

Related Questions