Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery .on('click', func) function not doing its job

Tags:

jquery

How to create the issue:

  1. click on existing trigger links - something happens (pass)
  2. click on the "add trigger" button, which appends a new trigger link w/ same class
  3. click on the newly created link, and it does not do the same thing as the 1st 2 links that existed from the start (fail)

Here is my example -: http://jsbin.com/equjig/3/edit

I thought the .on('click', func) function was good for adding events to current elements in the dom, but also for future elements that get added to the dom?

Here is my current javascript :

$(document).ready(function() {
  $(".link").on('click', function(e) {
    e.preventDefault();
    $("#out").append("clicked<br/>");
  });

  $("#b1").on('click', function() {
    $("#p1").append("<a href='#' class='link'>new trigger</a>");
  });

here is the HTML

<button type="button" id="b1">add trigger</button>
  <p id="p1">
    <a href="#" class="link">trigger 1</a>
    <a href="#" class="link">trigger 2</a>
  </p>
  <div id="out"></div>

Note - im using jQuery 1.7

Thanks!

like image 925
Ian Davis Avatar asked Nov 29 '11 18:11

Ian Davis


People also ask

Why click function is not working in jQuery?

jQuery click not working at the time page loading, jQuery Onclick Method is tried to an element or selector. As a result, the binding will fail if the element we wish to click isn't present when the page is ready.

Why click is not working?

To turn it on, go to Start > Settings > Devices > Mouse > Related Settings > Additional Mouse Options. The Mouse Properties window will pop up. At the bottom of the Buttons tab, you'll see the ClickLock options. Put a tick in the checkbox to enable 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 .click and .on (' click in jQuery?

on() differs from . click() in that it has the ability to create delegated event handlers by passing a selector parameter, whereas . click() does not.


2 Answers

You are almost there with on ...

working example : http://jsbin.com/equjig/6/edit

$(document).ready(function() {
    $('#p1').on('click','.link', function(e) {
        e.preventDefault();
        $("#out").append("clicked<br/>");
    });
});

This will work - you should select a parent div of the .link elements (i have used $('#p1') here, like in your jsbin)

like image 57
Manse Avatar answered Nov 04 '22 13:11

Manse


.on() can also behave like .delegate() which is used like this:

//#p1 is the context and .link is what will be bound to as long as the .link element is a descendant of #p1
$('#p1').on('click', '.link', function(e) {
    e.preventDefault();
    $("#out").append("clicked<br/>");
});

Here's documentation for .on(): http://api.jquery.com/on

like image 32
Jasper Avatar answered Nov 04 '22 13:11

Jasper