1

I tried to use more than one click handler for anchor tag . One using "Onclick" attribute handler and second using Jquery click handler .

This is my Html file .

<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script language="JavaScript" type="text/javascript">
    var count = 0;

    $("a[href*='google']").click(function(){
    count =count +1;
    alert(" google called" +count);
 });

  function clickThis()
   {
     count = count + 1 ;
     alert("its onclick Function call " +count);
   }
 </script> 
</head>

<body >
  <a id="foo" onclick="clickThis();" href="http://google.com">google</a>
</body>
</html>

It is running only "onclick" handler . I have checked that handler is right selecting the same anchor tag . But its click handler is not running .

It might be the case that jquery line would not be getting parsed .But as time of parsing the html in browser . it goes from top to bottom , so for script tag parsing , our jquery line would be parsed as like variable (count) declaration .

So what would be the reason that Jquery Click handler is not getting executed on clicking the URL .

And if it execute then what would be the execution order .

Thanks in Advance.

Virendra
  • 387
  • 1
  • 7
  • 15

3 Answers3

1

You cannot safely manipulate a page until the document is “ready.” via jqfundamentals

You'll need to wrap your code in a $(document).ready handler:

$(document).ready(function() {
    $("a[href*='google']").click(function(){
        count =count +1;
        alert(" google called" +count);
    });
});
chrisn
  • 2,095
  • 15
  • 20
  • Ok .but again my question is if parsing is done from top to bottom and all the code gets executed when it was not in any function body .Like I tried and count is working without having it inside the ready function . So why is it necessary to put it inside the ready function . 2 . What would be the execution order is decided if i also get more click handler using jquery – Virendra Jan 20 '12 at 13:44
  • ok right . As this time anchor tag was not parsed . So if we put the jquery below the tag . Would it work . i know this is very bad practice only want to clear the concept . – Virendra Jan 20 '12 at 13:51
  • It is generally considered better practice to put your script as low in the page as possible. See [this answer](http://stackoverflow.com/a/6026730/1134541) for a short explanation and [this answer](http://stackoverflow.com/a/1795502/1134541) for a more in-depth look. – chrisn Jan 20 '12 at 13:57
1

Add the following to $(document).ready():

$("a[href*='google']").click(function(){
    count =count +1;
    alert(" google called" +count);
});

So it becomes:

$(document).ready(function(){
    $("a[href*='google']").click(function(){
        count =count +1;
        alert(" google called" +count);
    });
});

The OnClick event is triggered first followed by the JQuery click event.

Sagar Patil
  • 1,938
  • 13
  • 21
0

You have to include you jQuery handler in $(document).ready....

This code should work:

<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script language="JavaScript" type="text/javascript">
    var count = 0;
      $(document).ready(function(){
        $("a[href*='google']").click(function(){
        count = count +1;
        alert(" google called" +count);
     });
    });

  function clickThis()
   {
     count = count + 1 ;
     alert("its onclick Function call " +count);
   }
 </script> 
</head>

<body >
  <a id="foo" onclick="clickThis();" href="http://google.com">google</a>
</body>
</html>
Fabrizio D'Ammassa
  • 4,729
  • 25
  • 32