0

I have a written a sample html

<html>
<head>
<title>Demo</title>
</head>
<body>

<h1> Event <h1>

/* <p> <a onmouseover = "alert('Pop  up window text')">  </a></p    
<p> <a href="http://www.google.com" onmouseover='++count; alert ("Moved")'> jjj</a></p>

</body>
</html>

But i am unable to display the count in alert can any one help me

Vivekh
  • 4,141
  • 11
  • 57
  • 102

5 Answers5

3

Because the "count" variable isn't declared, a new (local) variable will be instantiated each time the onmouseover event triggers. Change to:

<script type="text/javascript">
   var count = 0;
</script>
<h1> Event <h1>

<p> <a onmouseover = "alert('Pop  up window text, count:' + count)"> aaa </a></p    
<p> <a href="http://www.google.com" onmouseover="++count;"> jjj</a></p>

</body>
</html>

also, you seem to have a random /* in the demo you gave - check that your real code doesn't have something unexpectedly commented out.

You may find this question has some useful answers - so that you understand what's gone wrong this time :)

Community
  • 1
  • 1
Nathan
  • 6,095
  • 2
  • 35
  • 61
3

Check out this fiddle ive worked with your code only

http://jsfiddle.net/xf5gE/

Here is what i did

<html>
    <head>
        <script>
            var count = 0; 
        </script>
    </head>
<body>
<h1> Event <h1>
<p> <a onmouseover = "alert('Pop  up window text')">  </a></p>   
    <p> <a href="http://www.google.com" onmouseover='alert ("Moved :: Count = "+count); ++count'> jjj</a></p>

</body>
</html>
Ashwin Krishnamurthy
  • 3,750
  • 3
  • 27
  • 49
0

You can just do

alert(count);

to show the value of "count" in an alert box

Tobias
  • 7,238
  • 10
  • 46
  • 77
0

You need to initialize the count var before using the ++ statement.

Take a look at this : http://jsfiddle.net/92QQ4/

VAShhh
  • 3,494
  • 2
  • 24
  • 37
  • the declaration of the count variable needs to be outside that inline bit of js else it is reset every time, and therefore fails to count; – eyesathousand May 30 '11 at 10:03
0

You need to declare count and initialise it inside a code block

I would put '++count; alert("Moved")' into a separate function. I guess it's personal preference but I wouldn't have a multiple statement inline fragment of code.

Also you should use javascript: inside the tags to tell the browser its javascript. The browser will run without but it is probably screaming horrible things about your code in its error logs.

eyesathousand
  • 587
  • 2
  • 10