admin管理员组文章数量:1295898
/
i have
<div id="anettedata">
<div class="lbl">Annette Name</div><div class="txt"><input type="text" /></div>
<input type="button" id="mybtn" class="btnAdd" value="Add Another Annette" />
</div>
<div id="otheranettedata"></div>
and when the "Add Another Annette" click, i want place the same content in the div "anettedata". i have done this using the following jquery
$(document).ready(function () {
var datatoappend='<div class="otherdata">.........<input type="text" /></div>'
$('.btnAdd').click(function () {
$('#otheranettedata').append(datatoappend); // end append
});
});
My problem is that when i am again click on the newly created button "Add Another Annette" i want to place the same content below the newly created div. How can i rewrite this jquery and html to solve this problem please help me.
and i have one more question, when add new set of div, how can we remove "Add Another Annette"button from previous ones
http://jsfiddle/sF5av/1/
i have
<div id="anettedata">
<div class="lbl">Annette Name</div><div class="txt"><input type="text" /></div>
<input type="button" id="mybtn" class="btnAdd" value="Add Another Annette" />
</div>
<div id="otheranettedata"></div>
and when the "Add Another Annette" click, i want place the same content in the div "anettedata". i have done this using the following jquery
$(document).ready(function () {
var datatoappend='<div class="otherdata">.........<input type="text" /></div>'
$('.btnAdd').click(function () {
$('#otheranettedata').append(datatoappend); // end append
});
});
My problem is that when i am again click on the newly created button "Add Another Annette" i want to place the same content below the newly created div. How can i rewrite this jquery and html to solve this problem please help me.
and i have one more question, when add new set of div, how can we remove "Add Another Annette"button from previous ones
Share Improve this question edited Jan 3, 2014 at 7:50 neel asked Jan 3, 2014 at 7:31 neelneel 5,30312 gold badges49 silver badges67 bronze badges 1- 1 Be aware of that you're adding nodes with the same id as in your Fiddle. – Explicat Commented Jan 3, 2014 at 7:37
4 Answers
Reset to default 4Use event delegation.
The .on()
method attaches event handlers to the currently selected set of elements in the jQuery object.
Write:
$(document).on('click','.btnAdd',function () {
$(document).find('.btnAdd').remove();
$('#otheranettedata').append(datatoappend); // end append
});
Updated fiddle here.
Refer this document.
You need to use event delgation
$(document).on('click', '.btnAdd', function () {
$('#otheranettedata').append(datatoappend); // end append
});
DEMO
Use on() for dynamically added elements like,
$(document).on('click','.btnAdd',function () {
$('#otheranettedata').append(datatoappend); // end append
});
Demo
Updated to remove previously added button try this,
$('.btnAdd').length && $('.btnAdd').remove();
Updated Demo
Use jquery's .on method, so that it will bind events appended elements too.
$(document).on('click', '.btnAdd', function () {
$('#otheranettedata').append(datatoappend);
});
Above code will bind function to all elements with class '.btnAdd' in document.
本文标签: javascriptAppend div more than one time using jqueryStack Overflow
版权声明:本文标题:javascript - Append div more than one time using jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741623767a2388964.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论