0

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.

Sylver
  • 2,285
  • 3
  • 29
  • 40
  • 1
    If you are manipulating the dom outside of angular you need to tell it, look at `$apply`. – Dylan Oct 04 '14 at 00:22
  • 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-click`directive. – ryeballar Oct 04 '14 at 02:14

2 Answers2

0

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?

Dig
  • 1
  • 1
  • 1
  • 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 Oct 04 '14 at 01:04
0

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 https://stackoverflow.com/a/18994990/3612177

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

Community
  • 1
  • 1
Sylver
  • 2,285
  • 3
  • 29
  • 40