Skip to content Skip to sidebar Skip to footer

Count Div Elements With The Same Id Jquery

I created a more than one DIV with the same ID. I want to keep track of the number of DIV's by using JQUERY. This one does not seem to work. var divid = '123456'; var count_id_len

Solution 1:

Give class name for div and paste the below code.. It will give the count.

var conveniancecount = $("div[class*='conveniancecount']").length;

Solution 2:

Having same id multiple times is not recommended by W3C. See what they say about it.

The id attribute specifies a unique id for an HTML element (the id attribute value must be unique within the HTML document). The id attribute can be used to point to a style in a style sheet. The id attribute can also be used by a JavaScript (via the HTML DOM) to make changes to the HTML element with the specific id.

Suggestion:

Use class name for all the divs you want to group and use this selector:

$('.myClassName')

See these references for more:

1. Why do Ids need to be unique

Solution 3:

Maybe this can be useful for you:

I've been making inner ajax calls wich may be the container starts wrapping inside one time and another in some cases. I resolve it with this:

jQuery("div[id='yourWrapper_1']").length

Try unwrapping it, because you shouldn't have repeated ID's on your DOM

http://api.jquery.com/unwrap/

Hope it helps you

Regards!

Post a Comment for "Count Div Elements With The Same Id Jquery"