I am trying to implement html input array.
<input type="text" name="firstName[]" id="firstName[]">
And i need to set value of another form which looks something like
<form id="tempForm">
<input type="text" name="userName" id="userName">
<input type="text" name="userId" id="userId">
</form>
into the input array using jquery on form submit.
For that i tried following on form submit,
var currentIndex=$("input[name^=firstName]").length;
$("#firstName").eq(currentIndex).val($("#userName").val());
But it doesn't works,obviously.
Question:
How to set value of input array using jquery?
asked Nov 25, 2013 at 6:55
Runcorn
5,2245 gold badges37 silver badges52 bronze badges
1 Answer 1
Use the jquery append function for add inputs with different attribute value : Check it :
$(document).ready(function(){
var a = ["username","userid"];
var b = ["username","userid"];
for( var i = ; i <3 ; i++){
$('#tempForm').append('<input type="text" name="'+a[i]+'" id="'+b[i]+'" />);
}
});
Then continue your other work:
replace this code with your js code :
var currentIndex=$("input[name^=firstName]").length;
$("#firstName").eq(currentIndex).val($("#"+userName).val());
answered Nov 25, 2013 at 7:06
Vahid Taghizadeh
9978 silver badges9 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Aldi Unanto
$("#"+userName).val(). userName may an undifened variable.Runcorn
thanks for the suggestion @VahidTaghizadeh but i am looking for an input array implementation and it doesn't seems to follow that pattern.
Aldi Unanto
you miss a single quotes on this line ->
$('#tempForm').append('<input...default