Reputation: 182
So i have this page where people will be able to dynamically created inputs and select fields, and since i don't know how many of inputs and select fields will they create im kinda out of ideas on how would i get the values of every single one of them so i can send them to my php page via ajax.
I thought maybe a good idea would be to try to loop through every div and take values of select and input field, but i don't know how to do that. This is how html looks after user has created couple of inputs and seleect fields:
<div id="izmena_arhitekte">
<div class="headlinea">Arhitekte</div>
<div class="form-group">
<input type="text" class="form-control upis-style" placeholder="Ime i prezime" value="Zeljko Bogicevic" id="arh_ime1"> <i title="Obrisi Arhitektu" class="fa fa-times fa-2x deleteArh"></i>
<select style="margin-top:-18px;" id="tip1" class="form-control upis-style">
<option selected="" value="Projektant">Projektant</option>
<option value="Pomocni">Pomocni</option>
<option value="Nadzor">Nadzor</option>
</select>
</div><div class="form-group">
<input type="text" class="form-control upis-style" placeholder="Ime i prezime" value="Nikola Kojic" id="arh_ime2"> <i title="Obrisi Arhitektu" class="fa fa-times fa-2x deleteArh"></i>
<select style="margin-top:-18px;" id="tip2" class="form-control upis-style">
<option value="Projektant">Projektant</option>
<option selected="" value="Pomocni">Pomocni</option>
<option value="Nadzor">Nadzor</option>
</select>
</div>
<div class="form-group">
<input type="text" class="form-control upis-style" placeholder="Ime i prezime" value="" id="arh_ime3"> <i title="Obrisi Arhitektu" class="fa fa-times fa-2x deleteArh"></i>
<select style="margin-top:-18px;" id="tip3" class="form-control upis-style">
<option value="Projektant">Projektant</option>
<option value="Pomocni">Pomocni</option>
<option value="Nadzor">Nadzor</option>
</select>
</div>
</div>
Upvotes: 1
Views: 81
Reputation: 73221
Using javascript, you can find all the fields with querySelectorAll:
var a = document.querySelectorAll('input[type=text]');
for (var i = 0;i<a.length;i++){
console.log(a[i].value);
}
var b = document.querySelectorAll('option:checked');
for (var x = 0;x<b.length;x++) {
console.log(b[x].value);
}
});
You can, for example, put this in a EventListenerfunction and get all the entries on click on a button, or whatever Event you want to listen to
Upvotes: 0
Reputation: 36703
Basic approach
Code
var data = [];
$('div.form-group').each(function () {
data.push({
name: $(this).find('input').val(),
project: $(this).find('select').val()
});
});
console.log(data);
Upvotes: 0
Reputation: 767
Give a class to each input or select field which is added dynamically. For example, let's say I am giving the class name "inputField". Then to access all values:
var arr=[];
$(".inputField").each(function(){
arr.push($(this).val());
});
So now the arr array will have values of all dynamically inserted input fields.
Upvotes: 0
Reputation: 12391
You can use the $.each();
function of jQuery.
$(function() {
var collection = $('.form-group');
var data = [];
$.each(collection, function(idx, obj) {
data.push({'input': $(obj).find('input').val(), 'select': $(obj).find('select').val()});
});
console.log(data);
});
Upvotes: 2