chuckd
chuckd

Reputation: 14600

Select element with div and class and data attribute

How do I select a element using it's element type (Div), a class (spaceEvent) and a data attribute (data-event-id)?

here is an example of a element

<div class="spaceEvent" data-event-id=112>

and I want to find this element so that I can search for stuff inside it?

my guess would be something like

$("div.spaceEvent").find("[event-id=112]")

but I'm not sure!

Upvotes: 0

Views: 150

Answers (3)

Brett DeWoody
Brett DeWoody

Reputation: 62861

Simplest option:

$('div.spaceEvent[data-event-id="112"]')

Upvotes: 0

Chris Happy
Chris Happy

Reputation: 7295

You can use:

  • $('div.spaceEvent[data-event-id="112"]');
  • $('div.spaceEvent').filter('[data-event-id="112"]');

The find() function searches the children, not itself. More details here.

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337647

Your selectors are almost correct, but you need to combine them in to a single jQuery object. Your use of find() means that you're currently looking for a child element of the div instead of looking for a div with all the specified attributes.

Try this instead:

$('div.spaceEvent[data-event-id="112"]')

Upvotes: 1

Related Questions