3

I want to create below html inputs dynamically using jQuery inside the form. Is this possible using jQuery?

<div class="form-group">
  <label class="col-sm-2 control-label">Enter Name</label>
  <div class="col-sm-10">
    <input type="text" class="form-control" id="nameId" name="fullNme" placeholder="Enter full Name">
  </div>
</div>

I want to place the above dynamic div and texbox under div class="box-body"

<form id="myform" class="form-horizontal" method="post">
  <div class="box-body">


  </div>
</form>

Can someone help how can I achieve in Jquery/Javascript?

Appreciate your help in advance!

4
  • 1
    you want to create all the above elemets? Commented May 10, 2016 at 7:00
  • @Anoop Joshi - Yes Commented May 10, 2016 at 7:07
  • 1
    $('YOURCLICK').on('click',function(e){ $('box-body').append('<input type="text" class="YOURCLASS" name="YOURNAME[]">') }) Commented May 10, 2016 at 7:07
  • @javabegineer see my answer. Commented May 10, 2016 at 7:07

2 Answers 2

3

You can do,

var formgroup = $("<div/>", {
  class: "form-group"
});
formgroup.append($("<label>", {
  class: "col-sm-2 control-label",
  text: "Enter Name"
}));
var colsm = $("<div/>", {
  class: "col-sm-10"
});
var input = $("<input/>", {
  type: "text",
  class: "form-control",
  id: "nameId",
  placeholder: "Enter Full Namee"
});
colsm.append(input);
formgroup.append(colsm);
$(".box-body").append(formgroup);

Fiddle

You can give any number of attributes like this.

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

Comments

1

You can do like this:

<script>
    var html = '<div class="form-group"><label  class="col-sm-2 control-label">Enter Name</label><div class="col-sm-10"><input type="text" class="form-control" id="nameId" name="fullNme" placeholder="Enter full Name"></div></div>';

    $('.box-body').html(html);
</script>

Another way is:

<div id="container-bkp" style="display:none">
<div class="form-group">
  <label class="col-sm-2 control-label">Enter Name</label>
  <div class="col-sm-10">
    <input type="text" class="form-control" id="nameId" name="fullNme" placeholder="Enter full Name">
  </div>
</div>
</div>

<script>
    $('.box-body').html($('#container-bkp').html());
</script>

Comments

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.