1

on page load i have following div

<div id="row_log">......</div>

at this stage if i do console.log($('#row_log').length); then i get response as 1 which is correct.

however, i have button to clone the same div, which works fine. So if the button was clicked thrice, i would now have :

<div id="row_log">.....</div> original div
<div id="row_log">.....</div> cloned div
<div id="row_log">.....</div> cloned div
<div id="row_log">.....</div> cloned div

yet console.log($('#row_log').length); would still show as 1 instead of 4

Irfan Harun
  • 979
  • 2
  • 16
  • 37

2 Answers2

0

The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document). Use class.

Qui-Gon Jinn
  • 3,722
  • 3
  • 25
  • 32
0

Important points :
1. Id's must be unique.
2. In case of duplicate id's JavaScript will always get the first element for you, hence the length 1.

If you are just building a dynamic list or something, you can instead use a class.

......
And then do document.getElementsByClassName(className)

vS12
  • 310
  • 2
  • 8