最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create an HTML Input dynamically using jQuery? - Stack Overflow

programmeradmin8浏览0评论

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!

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!

Share Improve this question edited May 10, 2016 at 7:49 Irshu 8,4468 gold badges55 silver badges66 bronze badges asked May 10, 2016 at 6:58 java begineerjava begineer 3191 gold badge5 silver badges15 bronze badges 4
  • 1 you want to create all the above elemets? – Anoop Joshi P Commented May 10, 2016 at 7:00
  • @Anoop Joshi - Yes – java begineer Commented May 10, 2016 at 7:07
  • 1 $('YOURCLICK').on('click',function(e){ $('box-body').append('<input type="text" class="YOURCLASS" name="YOURNAME[]">') }) – Maninderpreet Singh Commented May 10, 2016 at 7:07
  • @javabegineer see my answer. – Anoop Joshi P Commented May 10, 2016 at 7:07
Add a ment  | 

2 Answers 2

Reset to default 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.

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>
发布评论

评论列表(0)

  1. 暂无评论