Skip to content Skip to sidebar Skip to footer

Obtain Values Of Array With Jquery

Let's say i have this form:
Field A

Solution 1:

Square brackets are used in jQuery selector and we should use escape characters to include the square bracket in id of control;

Live Demo

iterator = 0;
$('#field\\['+iterator +'\\]\\[a\\]').val();​

Solution 2:

The character "[" is not neutral in jquery selectors :

$('#field[0][a]') means : " select the items which have id "field", and also have an attribute named '0', and another attribute named 'a' "

You can :

  • either change your way to give an id to your fields, like : id="field_0_a", and thus have $('#field_0_a') work correctly,
  • or select your items by name : $('[name="field[0][a]"]')
  • or do like Adil said

Solution 3:

Add a class to inputs and do this

jQuery(document).ready(function($){
  $('.inputs').each(function(index, object){
    console.log($(object).val());
  });
});

On your code you need change here:

var v = $('#field[iterator][a]').val();

to:

var v = $('#field\\['+iterator+'\\]\\[a\\]').val();

Solution 4:

The problem is that you have to escape special characters like the following:

$('[id="field[' + iterator + '][b]"]').val(); or $('#field\\[' + iterator + '\\]\\[b\\]').val();demo

Post a Comment for "Obtain Values Of Array With Jquery"