Reputation: 152
I want to add a value to the input
field when it is append to DOM.
var strarray = [ "web developer", "web designer" ];
for (i = 0; i <= strarray.length-1; i++) {
j = [{ 'emp': strarray[i] }];
var a = j[0]['emp'];
console.log(a);
$("<input type='text' value=" + a + "/>")
.attr("id", "myfieldid"+i)
.attr("name", "myfieldid[]")
.appendTo(".mycal");
}
Upvotes: 8
Views: 143
Reputation: 25527
You can use
var input = $("<input/>", {
"type": 'text',
'value': a,
'id': "myfieldid" + i,
'name': "myfieldid[]"
});
$(".mycal").append(input);
Upvotes: 3
Reputation: 115212
You can create element using jQuery
var strarray = ["web developer", "web designer"];
for (i = 0; i <= strarray.length - 1; i++) {
j = [{
'emp': strarray[i]
}];
var a = j[0]['emp'];
console.log(a);
$("<input>", {
'type': 'text',
'value': a,
'id': "myfieldid" + i,
'name': "myfieldid[]"
}).appendTo(".mycal");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class=mycal></div>
Upvotes: 2