JavaScript Get the length of an array using document.getElementsbyName() array length using javascript,array length using jquery,array length

Hi I have an array like this.

 var i;
 for(i=0;i<10;i++){
     $('<input/>').attr('type','text')
                 .attr('name','TxtBx_[]')
                 .attr('id','TxtBx_' + i)
                 .attr("readonly","readonly")
                 .attr('value',i)
                 .addClass('txtbx')
                 .appendTo($div);
    }     

And I prints the 10 input boxes well.

Later I need to get the number of text boxes I have created. So I'm using

var myarr=document.getElementsByName('TxtBx_');
var numberofElements=myarr.length;

but when I put an alert to check the value of numberofElements it gives me always 0. The length of the array must be 10. Could someone please let me know the mistake I have made.

Answer:1

The elements' names are TextBx[], not TxtBx_.

var myarr=document.getElementsByName('TextBx[]');
var numberofElements=myarr.length;
Answer:2

Element names are TextBx[] and TxtBx_ is a class name

var myarr=document.getElementsByName('TextBx[]');
var numberofElements=myarr.length;

Read getElementsByName() documentation for more information

Answer:3
var numberofElements = document.getElementsByName("TextBx[]").length;

Name of textbox is 'name','TxtBx_[]' getting by TxtBx_.

Answer:4

Because no element has name TxtBx_ . It's TextBx[] actually.

Since you are alrady using jQuery, you can find by class like below,

$('.txtbx').length

Few other things, I would like to add here.

attr accepts a object too. So, you can pass all inputs at once. Also, you can pass attributes as second arguement while dynamically creating input. Also, according to jQuery docs, you should specify type in input type while dynamically creating them or it won't work in some IE.

So, try something like this,

var i;
for(i=0;i<10;i++) {
     $('<input type="text"/>',{
                 'name': 'TxtBx_[]',
                 'id': 'TxtBx_' + i,
                 'readonly':'readonly'
                 'value': i,
                 'class': 'txtbx'
     }).appendTo($div);  
}
Answer:5

Try it

$("input.txtbx").length;
Answer:6

Why doesn't the name function work. The chrome console outputs 'string is not a function'? Please provide the answer. Thanks <html> <head> <script type="text/javascript"> ...

Why doesn't the name function work. The chrome console outputs 'string is not a function'? Please provide the answer. Thanks <html> <head> <script type="text/javascript"> ...

  1. doesn't
  2. doesn't matter
  3. doesn't make sense
  4. doesn't equal sign
  5. doesn't remind me
  6. doesn't phase me
  7. doesn't really matter
  8. doesn't faze me
  9. doesnt afraid of anything
  10. doesn't synonym
  11. doesn't make sense now
  12. doesnt remind me chords
  13. doesn't suffer fools
  14. doesn't feel like christmas
  15. doesn't matter lyrics
  16. does not exist
  17. does not work
  18. does not equal
  19. doesn't care
  20. doesn't affect you

I declared my variable first - then have my jquery : var cimg = ''; jQuery(function($) { $(".trigger").click(function() { var cimg = event.target.id; clickedimage(); loadPopup(); }); }); ...

I declared my variable first - then have my jquery : var cimg = ''; jQuery(function($) { $(".trigger").click(function() { var cimg = event.target.id; clickedimage(); loadPopup(); }); }); ...

I have little problem with my webpage. When I'm trying to show InfoWindow on marker. That windows shows on the top left corner of a map instead of on the marker. Just like in screen below: Here is my ...

I have little problem with my webpage. When I'm trying to show InfoWindow on marker. That windows shows on the top left corner of a map instead of on the marker. Just like in screen below: Here is my ...

I used https://github.com/fatlinesofcode/ngDraggable for Drag&Drop in AngularJS. It works very fine, but I need to do some operations when the user is starting with dragging. This is myMethod(). I ...

I used https://github.com/fatlinesofcode/ngDraggable for Drag&Drop in AngularJS. It works very fine, but I need to do some operations when the user is starting with dragging. This is myMethod(). I ...

  1. call method starting new task
  2. abap call method starting new task
  3. sap call method starting new task