javascript - Why click event don`t work when re-append element? - Stack Overflow

Why click event not work in this code (JSFiddle: ):var holder = $("<div><div>")

Why click event not work in this code (JSFiddle: /):

var holder = $("<div></div>");

$(document.body).append(holder);

var btn = $("<button>Click Here</button>");

btn.click(function(){alert('clicked');});

holder.append(btn);
holder.html("");
holder.append(btn);

you can replace this line:

btn.click(function(){alert('clicked');});

with (Not work again):

btn.bind("click",function(){alert('clicked');});

and if i dont use jquery and set javascript event like this, it works fine!!

btn[0].onclick=function(){alert('clicked');}

Why click event don`t work when i re-append element (button) and how can i fix it?

Why click event not work in this code (JSFiddle: http://jsfiddle/3WeP5/):

var holder = $("<div></div>");

$(document.body).append(holder);

var btn = $("<button>Click Here</button>");

btn.click(function(){alert('clicked');});

holder.append(btn);
holder.html("");
holder.append(btn);

you can replace this line:

btn.click(function(){alert('clicked');});

with (Not work again):

btn.bind("click",function(){alert('clicked');});

and if i dont use jquery and set javascript event like this, it works fine!!

btn[0].onclick=function(){alert('clicked');}

Why click event don`t work when i re-append element (button) and how can i fix it?

Share asked Jul 13, 2013 at 10:59 Mehdi YeganehMehdi Yeganeh 2,1412 gold badges25 silver badges43 bronze badges 2
  • 1 use btn.on('click', function(){}); – Spirals Whirls Commented Jul 13, 2013 at 11:01
  • 1 @ Spirals Whirls Check it jsfiddle/3WeP5/2 not work.. – Mehdi Yeganeh Commented Jul 13, 2013 at 11:07
Add a ment  | 

3 Answers 3

Reset to default 6

Look the documentation of .html():

When .html() is used to set an element's content, any content that was in that element is pletely replaced by the new content. Additionally, jQuery removes other constructs such as data and event handlers from child elements before replacing those elements with the new content.

holder.html(""); is removing the handler of the button. If you want to keep it you can use clone as:

holder.append(btn.clone(true));
holder.html("");
holder.append(btn.clone(true));

Try on jQuery ON

// You are better off just adding a id to the button

var btn = $("<button id=\"someButton\">Click Here</button>");

$(document).on('click', '#someButton', function(){
   alert('clicked');
});

What this does is add's a click listener to the document and when someone click's the document it checks to see if the event.target === btn

Here is a demo

The reason that the event's don't work is because jQuery.html(); removes the event listeners it is confirmed here https://github./jquery/jquery/blob/master/src/manipulation.js#L231

JSFiddle

Use event delegation:

$(document.body).on('click', btn, function() {
    alert('clicked!');
});

Your code doesnot work because when the script if first loaded it binds the function to that particular element but when you add another one the script doesnot run again and so the appended element doesnot get its event binded. When we add event delegation, we target document.body and so when the script gets loaded event gets binded to that element on the body.

Demo.

For earlier versions of jQuery you may use this one:

$(document.body).delegate(btn, 'click', function() {
    alert('clicked!');
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742266161a4411825.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信