0

The linked duplicate explains you can only use an id once, but if you read my code, each id is unique. From the linked question:

Each id value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. This behavior should not be relied on, however; a document with more than one element using the same ID is invalid.

This however is not what my code below is doing. Before being so quick on the close trigger, please read the question or at least provide a link to an answer that actually addresses my question.

Original post:

Can someone explain why when using the code below, in the console it lists each div as "1" and a separate line item such as:
1
1
1
1
instead of logging "4"

I am trying to count the number of divs with the id that matches. It is not inside of a .each or anything

success: function (data) {
           var n = jQuery('#name_' + cid ).length;
            console.log(n)
}

This outputs a <div id=name_15> with a number that changes and is unique

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
user14955679
  • 181
  • 8
  • 2
    when you want multiple elements access with one selector then use class and not id – Aalexander Jan 23 '21 at 21:07
  • 2
    The id's are unique because of the `id`. Classes arent going to be unique – user14955679 Jan 23 '21 at 21:08
  • 2
    If I understood correctly, you can do something like: `var n = jQuery('.'+jQuery('#name_'+cid).attr('class')).length;` Because when you get an element with id it just returns the length of one element with this id. but in my code, you get the class name from id (I assume class names are same), so it returns the count of elements with same class name. – Javanshir Huseynli Jan 23 '21 at 21:16
  • 1
    ok thank you. Now that is an answer to the question and is not covered in the link above. I wish I could accept that as the answer and makes sense. I appreciate you – user14955679 Jan 23 '21 at 21:18
  • 1
    Yeah, that's the bad thing about here, someone can just close the question before the asker gets an answer. That's not cool and the reason I don't ask questions. Anyway, glad to help. Good luck. – Javanshir Huseynli Jan 23 '21 at 21:20
  • 2
    @user14955679 The fact that 2 comments were made about IDs needing to be unique, and that someone closed your question for that reason shows that your question does not include enough details. Please edit your question to make it more clear _(include some example HTML)_, and I'll be glad to vote to reopen it (needs 3 votes for it to happen) – blex Jan 23 '21 at 21:25
  • You arent reading given that the ID is unique. Each id is unique if you look at my code. It really couldnt be more clear – user14955679 Jan 23 '21 at 21:33
  • It could be way more clear. Anyway, glad you found a solution – blex Jan 23 '21 at 21:35
  • 1
    Editing the question to provide sufficient detail in the post to allow readers to reproduce the problem may have helped getting it reopened. – traktor Jan 24 '21 at 00:15

0 Answers0