40

This may be a bit of strange case where nobody has ever experienced this before, but I'm posting this on the off-chance that somebody knows something I don't.

I'm using jQuery 2.0.3 and AngularJS.

If I have an anchor in index.html like so:

# index.html
<a href="#" class="clickme">Click Me</a>

<script type="text/javascript">
$(function() {
    $('.clickme').click(function() { console.log("click"); });
});
</script>

Then it works, and when I click it, it outputs 'click'. But when I put that inside a template that I include with the ng-include attribute, the jQuery all of a sudden doesn't fire. If I place the script inside the template with the anchor though, it DOES fire.

# index.html
<div ng-include="'path/to/template.html'"></div>

# template.html
<a href="#" class="clickme">Click Me</a>

<script type="text/javascript">
$(function() {
    $('.clickme').click(function() { console.log("click"); });
});
</script>

This is also the case with using directives which use templates. It's bizarre and causing me a lot of hassle with some drop down menus.

Does anyone know why this is happening?

6
  • 1
    Probably something to do with the DOM not being ready at time of binding, BUT, you should put all dom manipulation into directives!!!!! Commented Sep 18, 2013 at 15:36
  • can you put code on jsfiddle/plunker Commented Sep 18, 2013 at 15:37
  • This is a simplified case, in actuality it's a javascript file that is called in the index.html that wants to handle the navigation menu. Commented Sep 18, 2013 at 15:37
  • 2
    You really should use a directive for this. Commented Sep 18, 2013 at 15:50
  • 1
    I was, I just simplified my example. It was a directive that wasn't firing js events. Commented Sep 19, 2013 at 8:46

1 Answer 1

101

Since your clickme probably isn't available on DOM ready - rather it is pushed into the DOM by Angular when it loads template.html - you need to use jQuery's event delegation instead of using .click:

$(document).on("click", ".clickme", function() {
  console.log("click");
});

This will ensure jQuery binds onto the document, then monitors for any click events on elements with the clickme class.

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

5 Comments

This works, thanks. Looks like the bootstrap theme I'm using wasn't designed for Angular. I reckon I can upgrade it with this though. Thank you!
No problem. The $(document) selector is just an example, it's better if you bind onto a parent element of .clickme - obviously one that isn't also generated by Angular :)
Thanks, I bound it to the smallest parent that was inside index.html, all working now. Marked as answer. Thanks again!
Works partially for me but the delegate event is not firing in first click, could you help me on this?
Not working for me: $(".bordered-button-red .show-guide-button").on("click", function () { alert("Clicked!"); });

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.