jQuery: clone elements AND events

老子叫甜甜 提交于 2019-11-30 17:35:35

.clone( true ) does the trick.

Documentation: http://api.jquery.com/clone/

Hi I'm having a bit similar use case, I have some dynamically generated content that contains a button, click event is responding to the original button but not the generated one, I've done before :

$('.someclass').on('click', function() {

but i resolved my problem by replacing the on by live like this :

$('.someclass').live('click', function() {

If your handlers are setup using something like $('.class').click( ... )

Try something like this instead: $('.class').live('click', ...)

Live applies to elements with the class that may not exist yet.

I finally got the UI datepicker to work properly. I had to completely remove datepicker BEFORE cloning and add it AFTER cloning elements. The guys at UI should make it easier for us. Go figure!

Just before cloning:

//#datepicker
$("input[name='date']").datepicker( "destroy" );
$("input[name='date']").removeClass("hasDatepicker").removeAttr('id');

After cloning:

$("input[name='date']").datepicker({ dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true, numberOfMonths: 3, showButtonPanel: true});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!