javascriptjqueryarrayssquare-bracketname-attribute

length of array used for html input element's name attribute


This bit of code below appends a div to another div. The former div contains an input element with the name attribute "inputName[]". I'm guessing this is an array that auto-increments as more divs are added. How can I output the length of this array? I tried to console.log it (as below), but it's not working.

The error I get is "Uncaught TypeError: Cannot read property 'length' of undefined"

What can I do to fix this please?

$("#buttonid4").on("click", function() {

    var string1 = "<div class='eight columns'>
       <input type='text' size='76' placeholder='paste URL here' name='inputName[]'> </div>";

        $("#deleteandadd").append(string1);                            

        console.log(inputName.length);

    });

Solution

  • if you want number of inputs with name "inputName[]" ,you can use name selecter in jquery.

    like the following

    $('#deleteandadd input[name="inputName[]"]').length
    

    or

    var  inputName=    $('#deleteandadd input[name="inputName[]"]')
    
    console.log(inputName.length)