Getting multiple values of equal inputs and storing in an array with Javascript / jQuery

1

I have this div, which is <div class='pergunta'> , inside it I have <input type='text'> <select></select> <input type='checkbox'> The question is that this entire div can be repeated if I click add new question, ie I can have 1 or N divs containing these same fields, I need to do a Javascript / jQuery script that takes the value of each field and stores it in an array, for example, I have two <div class'pergunta'> with these fields, I need to add the values of each field in an array by example:

var array = [{titulo: , tipo: , requerido: }, {titulo: , tipo: , requerido: }]

That is, how do I add the values of these fields of each div to an array?

    
asked by anonymous 18.11.2017 / 18:50

1 answer

0

Create a selector that collects all <div class='pergunta'> and then makes a mapping to fetch the title and type values.

An example would look like this:

const arr = $('.pergunta').map(function() {
  return {
    titulo: this.querySelector('input[type="text"]').value,
    tipo: this.querySelector('select').value,
    requerido: this.querySelector('input[type="checkbox"]').checked
  }
}).get();
console.log(arr);
    
18.11.2017 / 19:36