Count div elements with the same jquery id

I created more than one DIV with the same identifier. I want to track the number of DIV using JQUERY . This does not seem to work.

var divid = "123456"; var count_id_len = $("#parentdiv").find("#"+divid).length; console.log(count_id_len); 

The result is always 1 , although there are more than one of them.

+7
jquery html css
source share
3 answers

Enter the class name for the div and paste the code below. This will give the score.

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

Having the same identifier several times is not recommended by W3C. See what they say about it.

The id attribute indicates a unique identifier for the HTML element (the value of the id attribute must be unique in the HTML document ). The id attribute can be used to specify a style in the stylesheet. The id attribute can also be used by JavaScript (via the HTML DOM) to make changes to an HTML element with a specific identifier.

Sentence:

Use the class name for all sections that you want to group, and use this selector:

 $('.myClassName') 

See these links for more details:

1. Why identifiers should be unique

+3
source share

Perhaps this may be useful for you:

I make internal ajax calls, which can be a container that starts wrapping in one go, and another in some cases. I resolve this with this:

Find the container container with the request with this selector instead of "#yourWrapper":

JQuery ("DIV [ID = 'yourWrapper_1']"). Length

Try expanding it because you do not have to have a duplicate identifier on your DOM

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

Hope this helps you

Hello!

+1
source share

All Articles