0
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.

asked Mar 7, 2012 at 13:38
3
  • 3
    So why you marked as accepted your previous analogue answer? :o Commented Mar 7, 2012 at 13:40
  • its working on first condition when i marked.but when i check with second it is not working. Commented 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. Commented Mar 7, 2012 at 13:51

2 Answers 2

1

do is a reserved word in javascript. Try naming the method something else.

answered Mar 7, 2012 at 13:44
Sign up to request clarification or add additional context in comments.

4 Comments

Even not working, I tried wit the name echo '<a onclick="doin();"><img src= "1.jpeg"></a>';
@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
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.
@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.
1

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>';
answered Mar 7, 2012 at 13:46

3 Comments

Please return false onclick instead of javascript:void(0)
@mplungjan: I hope the edit is what you suggested that should have been done ?
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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.