Tell me, please, how can the values ​​of the fields of this form be written to an array?

<form> <input type="text" value="q" /> <input type="text" value="3e" /> <textarea rows="20" cols="20"></textarea> <input type="submit" value="submit" /> </form> 

    3 answers 3

    You can like this:

     $( function(){ var arr = [] // Объявим массив // Выберем все элементы формы $('form').find('input, textarea, select').each( function(){ // Добавим значение в массив arr.push(this.value) }) alert(arr.join(', ')) })​ 

    Example 1

    And you can, and even shorter :)

     $( function(){ var arr = $('form').find('input, textarea, select').map( function(){ return this.value }).get() alert(arr.join(', ')) })​ 

    Example 2

      It's generally good to use $ ('form'). serializeArray () , but this method will not work without the name tag.

         var forms = document.querySelectorAll('input'); for (var i in forms) { console.log(forms[i].value); //или меняем forms[i].value = "fill"; } 

        // textarea script will capture