Skip to content Skip to sidebar Skip to footer

Jquery - How Do I Detect How Many Divs Of A Given Class Exist Within A Given Div?

I have a div like this:
and contained within this div I have several divs like this:
).length; // number of class y elements under element x $('#x div.y').html('Y'); // run a jQuery method on the y elements

See the API:

Solution 2:

alert($('.x .y').length);

$('.x .y').html('Y');

Solution 3:

//instead of $('#x .y') you can also use $('#x').find('.y')alert($('#x .y').length())

$('#x .y').each(function(){
    //do what you want to $(this)
    $(this).html('Y');
});

Solution 4:

Try this.

alert($('.x .y').length)

http://api.jquery.com/length/

Solution 5:

1. The length() method gets the total amount of returned elements:

alert($('.x .y').length());

2. You were correct in how to set the content on all of the returned elements:

$('.x .y').html('Y');

Post a Comment for "Jquery - How Do I Detect How Many Divs Of A Given Class Exist Within A Given Div?"