I learned that how to dynamically add input fields to a form by using jquery append. My question is: How can read these fields? for example: I want from my visitor to enter a number and I for each number create an input, how read it when I don't know about name of fields and number of them? And an important problem is how can I naming them. please help me
We can do naming and reading fields that create dynamically these is your answer:
<div>
<input type="radio" id="radio_1" name="radio1" value="1" /> 1<br />
</div>
and in Js:
for(i=2;i<=8;i++){
$("div")
.append($("#radio_1")
.clone().attr({"id":"radio_"+i,"value":i}))
.append(" "+i+"<br />");
}
$(":radio").click(function() {
var radioID = $(this).attr("id");
alert("by selector (:radio): " + radioID);
});
$("input[name=radio1]").click(function() {
var radioID2 = $(this).attr("id");
alert("by groupname: " + radioID2);
});
you and commentators can see it in jsfeedle
It's hard to say without seeing the code exactly but say it's something like
$('body').append('<input type="text" class="readMe">');
You could grab the values of those with something like this:
$('.readMe').each(function(){
$(this).val();
});