0

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!

1
  • What happened when you tested it? Commented Jul 5, 2017 at 21:47

3 Answers 3

1

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"]')
Sign up to request clarification or add additional context in comments.

2 Comments

why do I need to put the number (112) inside quotes? its a number not a wiord
You don't have to in this case, but it's a better convention. The type of the value is not relevant to the selector, but if there is a space in the value you have to wrap it in quotes.
0

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.

3 Comments

.is() just returns a boolean telling if the element has the data, it doesn't return the element.
Quotes are only required if the attribute value contains special characters.
Opps... Didn't know that. Thanks!
0

Simplest option:

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

1 Comment

The .find() version is wrong. .find() only looks for elements inside, not the element itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.