21

How can I get an element by id, and then inside this element get all elements by class name using jQuery? It's pretty easy to do this using the standard JS functions getElementById() and getElementsByClassName(), but unfortunately IE 7-8 do not support the latter.

3 Answers 3

37

You have a few options:

The first, using a css selector:

$('#idOfElement .classNameOfElements');

Or using find():

$('#idOfElement').find('.classNameOfElements');

Or using selector context:

$('.classNameOfElements', '#idOfElement');

It's worth noting that using the context (final) approach causes jQuery to internally implement the find() method.

References:

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

Comments

6
var byID = $("#someid");
var byClass = byID.find(".someClass");

Comments

2

In jquery you can get element by id as $('#some_id') and get element by class name as $('.some_class_id') please see jquery api for more details.

and to access inside elements you can do it like this $('#some_id .some_class')

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.