8

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");
}
2
  • 1
    Its working. jsfiddle.net/rzuc9e2d One thing, instead of $("<input type='text' value=" + a + "/>") use $("<input type='text' value='" + a + "'/>") That is required if string "a" contains space, which in you case is there. Commented Sep 18, 2015 at 9:20
  • @RORY Its not working please check. and i have already try with this.jsfiddle.net/rzuc9e2d Commented Sep 18, 2015 at 9:44

2 Answers 2

3

You can use

var input = $("<input/>", {
    "type": 'text',
    'value': a,
    'id': "myfieldid" + i,
    'name': "myfieldid[]"
});

$(".mycal").append(input);

Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

see the fiddle provided
2

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>

1 Comment

@sourabh : glad to help :)

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.