echo '<a onclick="alert(\'alert\');"><img src= "1.jpeg"></a>';
It is working fine, But
echo '<a onclick="do();"><img src= "1.jpeg"></a>';
Its not working if i put that alert inside do() like
<script type="text/javascript">
function do()
{
alert("alert");
}
</script>
Can any body help on this.
-
3So why you marked as accepted your previous analogue answer? :oDamien Pirsy– Damien Pirsy2012年03月07日 13:40:36 +00:00Commented Mar 7, 2012 at 13:40
-
its working on first condition when i marked.but when i check with second it is not working.AshokiPhone– AshokiPhone2012年03月07日 13:45:15 +00:00Commented Mar 7, 2012 at 13:45
-
I am new to this site..Just i just confused with this comment styles and options.And i just missed the link after i gave the mark.AshokiPhone– AshokiPhone2012年03月07日 13:51:30 +00:00Commented Mar 7, 2012 at 13:51
2 Answers 2
do is a reserved word in javascript. Try naming the method something else.
answered Mar 7, 2012 at 13:44
AndersDaniel
1,1609 silver badges20 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
AshokiPhone
Even not working, I tried wit the name echo '<a onclick="doin();"><img src= "1.jpeg"></a>';
Janis Veinbergs
@AshokiPhone You can use Chrome web inspector or Firefox Firebug to read what errors javascript parser gives to you. It should be a great help for you
AndersDaniel
Then it sounds like the javascript isn't loaded as it should be. Could you update your original question with some detail on the context in which you use the code? How do you load the javascript etc.
Travesty3
@AshokiPhone: With the code in your original post, it does not work for me. If the only thing that I change in your code is to change the function name to
doin and update the call to match, it does work.You can't have a function named do(). Change the name to something else. Also, it would be a good idea to use something like:
echo '<a href="javascript:void(0)" onclick="your_function();return false;">
<img src="img.jpg">
</a>';
3 Comments
mplungjan
Please
return false onclick instead of javascript:void(0)gentrobot
@mplungjan: I hope the edit is what you suggested that should have been done ?
mplungjan
Yes, but retain the href - I like href="#" but others at SO do not since it does reload the page but ONLY if javascript is turned off because of the return false
default