0

I have gone thought stackoverflow for a solution to my jQuery click function only firing once on the same page load and noticed that the solutions provided only apply to specific element referencing.

My jQuery listens for a tag request (<a href=...>) So does anyone know how to make my listener fire multiple times while listening to this type of tag?

Current Code

$("a").click(function() {
    if($(this).attr('href') != '#' && $(this).attr('href') != '#myModal'){
        displayLoader();
    }
}
Beanonymous
  • 95
  • 2
  • 9
  • see http://stackoverflow.com/questions/20463959/jquery-click-function-only-working-once-new-situation?rq=1 – Santhosh May 06 '14 at 06:02

3 Answers3

0
$("a").click(function(ev) {
    ev.preventDefault();
    if($(this).attr('href') != '#' && $(this).attr('href') != '#myModal'){
        displayLoader();
    }
}

ev.preventDefault() will prevent the default behavior of the click, and proceed with the custom code that you had create, you can find more explanation here http://api.jquery.com/event.preventdefault/

Se0ng11
  • 2,303
  • 2
  • 26
  • 45
  • Answers with just a code block and no explanation generally get flagged for deletion. I would add some details as to how your code solves the problem! – sgress454 May 06 '14 at 06:21
  • I thought the same thing. it is also essentially the same as the first answer provided. – Beanonymous May 06 '14 at 06:29
0

"a" tags default event is it will process the link(href) when clicked. So you need to prevent that by=

$("a").click(function(e) {
     e.preventDefault();
     //rest of the codes to handle the click
});
Tuhin
  • 3,335
  • 2
  • 16
  • 27
-1

Try this:

  $("a").on("click",function() {
if($(this).attr('href') != '#' && $(this).attr('href') != '#myModal'){
    displayLoader();
   }
 }
Naveen Chandra Tiwari
  • 5,055
  • 3
  • 20
  • 26