0

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?

1 Answer 1

4

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());
Sign up to request clarification or add additional context in comments.

3 Comments

$("#"+userName).val(). userName may an undifened variable.
thanks for the suggestion @VahidTaghizadeh but i am looking for an input array implementation and it doesn't seems to follow that pattern.
you miss a single quotes on this line -> $('#tempForm').append('<input...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.