mykola
mykola

Reputation: 1808

Get elements by class name inside another element with jQuery

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.

Upvotes: 21

Views: 51160

Answers (3)

Aamir Rind
Aamir Rind

Reputation: 39709

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')

Upvotes: 2

David Thomas
David Thomas

Reputation: 253485

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:

Upvotes: 38

Dogbert
Dogbert

Reputation: 222398

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

Upvotes: 7

Related Questions