Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The second .click() function is not working

Tags:

jquery

The first .click function is used to add an element(div) in a container and the second one is used to remove it from the container. The container initially has no elements. The removing of the function by clicking on it is not working

$(document).ready(function(){
    $(".class1").click(function(){
       //Code for adding element to the container and 
       // removing class1 from it and adding class2

    });

    $(".class2").click(function(){
       alert("hi");   //Even the alert is not displayed
       $(this).fadeOut(100);    
    });
});

However, it works if the element is already there before the page loads in the container. Any reasons why? Is it because of document.ready function? Solutions?

like image 948
Mr Boss Avatar asked Jun 22 '12 10:06

Mr Boss


People also ask

Why button on click is not working?

On Windows 10, head to Settings > Devices > Mouse. Under “Select your primary button,” ensure the option is set to “Left.” On Windows 7, head to Control Panel > Hardware and Sound > Mouse and ensure “Switch primary and secondary buttons” isn't checked. The ClickLock feature can also cause strange issues.

What is Click () method?

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 you trigger a click element?

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.

What is the difference between .on click function ()) and .click function?

. click events only work when element gets rendered and are only attached to elements loaded when the DOM is ready. . on events are dynamically attached to DOM elements, which is helpful when you want to attach an event to DOM elements that are rendered on ajax request or something else (after the DOM is ready).


3 Answers

That's because when you're adding the click handler for .class2 elements, you're only adding the event to elements which have that class at that specific moment in time; e.g none.

Instead, you need to use event delegation like so;

$(document).on('click', '.class2', function () {
    alert('hi');
    $(this).fadeOut(100);
});

This will work as it binds an event to document (which always exists), which listens for clicks on any .class2 elements using event delegation. For more info, read the on() documentation.

like image 80
Matt Avatar answered Sep 28 '22 22:09

Matt


use delegate event handler for both classes(if you toggle between both of them, or if you not back to class1 then second one is enough ) because after changing class elements are treated as dynamic.

$("#container").on("click", ".class1", function(){

});

$("#container").on("click", ".class2", function(){

});

Here #container refers to the parent of those class, you may have something else.

like image 39
thecodeparadox Avatar answered Sep 28 '22 22:09

thecodeparadox


As you try to add the code to click .class2 it isn't yet created as i understand. try adding the click event, after you created the .class2 element like this:

$(document).ready(function(){
    $(".class1").click(function(){
            //Code for adding element to the container and removing class1 from it and adding class2

        $(".class2").click(function(){
            alert("hi");         //Even the alert is not displayed
            $(this).fadeOut(100);

        });
    });
});
like image 45
devsnd Avatar answered Sep 28 '22 22:09

devsnd