Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.click() fails after dom change

Tags:

jquery

dom

click

I searched on web but I didn't find any answer since this "problem" is not the usual one about the difference between .on() and .click(). With jquery 2.1.3 the click function is a shortand for on.("click", handler) so it should fire a function (or wathever) after the dom is changed. But this works only if I use .on(). Why? (Example below)

$('#button1').click(function() {
    $('div').html("<p id="button2">Hello</p>");
});

$('#button2').click(function() {
    alert(0); //THIS DOESN'T WORK
});

$(body).on("click", "#button2", function() {
    alert(0); //THIS WORKS!
});
like image 851
Giacomo Cerquone Avatar asked May 02 '15 08:05

Giacomo Cerquone


People also ask

What is click() in html?

The click() method simulates a mouse-click on an element. This method can be used to execute a click on an element as if the user manually clicked on it.

How do I load CSS after DOM load?

you can use jquery as a solution for this... put all the css changes you want your #devicelayout to happen after page load into a css file for example devicelayout. css. then in your script tag (ofcourse after you include the jquery library) add this code...

How to click an html element in JavaScript?

The HTMLElement. click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.

How click event works?

An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element.


3 Answers

$('#button1').click(function() {
    $('div').html("<p id='button2'>Hello</p>");
});

$('#button2').click(function() {
    alert(0); //THIS DOESN'T WORK
});

$(document).on("click", "#button2", function() {
    alert(0); //THIS WORKS!
});

This is correct code

https://jsfiddle.net/hhe3npux/

like image 188
Manish Shukla Avatar answered Oct 16 '22 09:10

Manish Shukla


But this works only if I use .on().

First of all , you should realize that :

If

$('#button2').click(function() {
    alert(0); 
});

comes after

$('#button1').click(function() {
    $('div').html("<p id="button2">Hello</p>");
});

like :

   $('#button1').click(function() {
        $('div').html("<p id="button2">Hello</p>");
        $('#button2').click(function() {
          alert(0); 
      });
    });

Then it WILL work.

the thing which you did in the last code is attaching the handler to the body element which is working because of event propagation.

your code is working beacuse on allows you to do selector matching + attaching single handler to the body element.

like image 41
Royi Namir Avatar answered Oct 16 '22 09:10

Royi Namir


The answer is if you added data dynamically, then you must use .on function. With this code, you can use event delegation concept by using the code that you mention at last. Since the DOM are not registered yet, the .click handler cant capture the new DOM element.

like image 35
Norlihazmey Ghazali Avatar answered Oct 16 '22 09:10

Norlihazmey Ghazali