Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an HTML Input dynamically using jQuery?

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!

like image 971
java begineer Avatar asked Aug 16 '26 05:08

java begineer


1 Answers

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.

like image 83
Anoop Joshi P Avatar answered Aug 17 '26 18:08

Anoop Joshi P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!