0

How do I do the below with javascript?

$("div.details.counter span")

I can use ID's but I'm curious how we can do the above jQuery style of getting stuff with pure javascript.

5
  • 2
    Document.querySelectorAll() Commented Jan 16, 2014 at 13:06
  • I am not familiar with jQuery. Could you please explain what the above expression does? Commented Jan 16, 2014 at 13:08
  • @KeyurGolani it targets all the span tags inside all the div tags that have a class of both details and counter Commented Jan 16, 2014 at 13:09
  • @Deadpool "inside" i hope means "inside the DOM subtree of". I would describe that selector as targetting all the span elements which have a div ancestor with both details and counter class. To avoid confusion regarding the child selector. Commented Jan 16, 2014 at 13:19
  • @Tibos what did you expect. It was translated by deadpool ;) Commented Jan 16, 2014 at 13:22

1 Answer 1

2

The solution with pure javascript for most selectors supported by jQuery is to use document.querySelectorAll:

document.querySelectorAll("div.details.counter span")

If you don't have support for querySelectorAll in your browser of choice, you could take a look at jQuery's implementation of the selector engine to see how it matches various parts of the selector.

Sign up to request clarification or add additional context in comments.

Comments

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.