1

After initialize js I create new <div> element with close class and on("click") function doesn't work.

$(document).on('click', '.post-close', function () {
        alert("hello");
});

but on('hover') work perfectly.

$(document).on('hover', '.post-close', function () {
        alert("hello");
});

but I need to make it work on click.

4
  • 7
    I think you posted the same code twice. Commented Oct 18, 2012 at 13:35
  • Also, is it on(hover) or live(hover)? Commented Oct 18, 2012 at 13:35
  • I would also try running your code through the W3C validator, sometimes DOM issues can cause problems with javascript events. validator.w3.org Commented Oct 18, 2012 at 13:36
  • Works fine here jsfiddle.net/2MdRR Commented Oct 18, 2012 at 13:44

3 Answers 3

4

It's because you're not preventing the default behaviour of the browser. Pass e into your handler and then use e.preventDefault()

$(document).on('click', '.post-close', function (e) {
    e.preventDefault();
    alert("hello");
});

Edit

Also, bind the handler before creating the new <div>

Sign up to request clarification or add additional context in comments.

1 Comment

do you get any errors in the console or anything? What if you console.log() instead of alert?
1

why not use something like

$('.post-close').click(function(){
   //do something
});

If the element was added dynamically use:

$(document).on('click', '.post-close', function(){
   //do something
});

edit:
like danWellman said, you can add the preventDefault IF you want to make sure no other code is executed. otherwise use the code above.

edit2:
changed the .live to .on

3 Comments

.live(); is deprecated, use .on(); instead: "As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live()."
@GeertJaminon: thanks for the update. Guess I'm a bit behind on my jquery versions.
@Ruben ~ not only that, actually, but you posted what is essentially the same code. I think what you wanted to share was that for dynamically added elements, you want to use the delegate flavor of .on(). That has a syntax of container.on(eventtype, selector, handler).
0

It's an old post but I've had a exactly same problem (element created dynamically, hover works, but click doesn't) and found solution. I hope this post helps someone.

In my case, I found ui-selectable is used for parent element and that was preventing from click event propagate to the document.

So I added a selector of the button element to ui-selectable's 'cancel' option and problem solved.

If you have a similar probrem, check this

  • Try turn of libraries for parent element
  • You're not using stopPropagation() in parent element ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.