Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm lost about jquery selectors and events.

I'm trying to add a click behavior on a bootstrap table-hover, however the .click isn't triggered and i don't know why.

I've tried :

  $( ".table-hover tbody tr" ).click(function() {
    alert("clicked");
  });

and :

  $( ".table-hover tbody" ).on( "click", "tr", function() {
    alert("clicked");
  });

as found around the web, but none of these triggered the click.

I tried to test the selector with a simple img tag :

  $( "img" ).click(function() {
    alert("clicked");
  });

This one works ... only on the first image of the document, but the other ones come from angularjs behaviors, like ng-repeat and ng-model after querying an api, i'm sure that's related but i don't know what's the problem for jquery since the .on() method should do the trick.

I've read about the .on() which should add event even for elements added after the document is loaded, but no solution is working for me so far.

share|improve this question
1  
If you are manipulating the dom outside of angular you need to tell it, look at $apply. –  Dylan 2 days ago
1  
What are you trying to accomplish? if you are creating custom behaviours for particular elements then use a directive and add your events in that directive and then attach it to the elements that you want, if not then simply use the ng-clickdirective. –  ryeballar 2 days ago

2 Answers 2

The code what you are giving is not angularjs. It is JQuery. I tried the first example you pasted, it is working fine. $(".table-hover tbody tr").click(function() { alert("clicked");});

Is your intention is to use angular, instead of JQuery?

share|improve this answer
    
I know this is jquery, that's in the title. But it is used along with angularjs. And since the code doesn't work while it should, i'm only making an assumption that it's related to angularjs with jquery. –  Sylver 2 days ago
up vote 0 down vote accepted

I'm new to angularjs and i was leading in the wrong direction by wanting to use bare jquery events while having angular.

Following advices from @dylan and @ryeballar, I used a directive instead to add a custom attribute, like explained here http://stackoverflow.com/a/18994990/3612177

Useful links about the subject too : http://jimhoskins.com/2012/12/17/angularjs-and-apply.html

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.