views:

59

answers:

4
pic_display+="<img src='"+pic_url+"' onClick='swapImage("+pic_url+");'>

I am adding an image to the innerHTML of a div with the code above. I want to call the function swapImage(pic_url) whenever I click on the image.

I have still not figured out the syntaxes in javascript, always confuse them with php.

How should it be written ?

Thanks

A: 

You need to wrap the function argument in quotes like you have with the src attribute. For example.

  pic_display+="<img src='"+pic_url+"' onClick='javascript:swapImage(\""+pic_url+"\");'>
Yacoby
There is no requirement to put javascript: in an onclick like that
jarrett
Mike Samuel
A: 

Should be like this onClick='swapImage(\""+pic_url+"\");'

Note you need to quote the pic URL when it is the argument since you are meaning to use a string and not a variable.

jarrett
A: 

Instead of

onClick='swapImage("+pic_url+");'>

try

onClick='swapImage(\""+pic_url+"\");'>
Miguel Ventura
+1  A: 

You could dump the quoting issue by using a variable instead of the string:

... onclick="swapImage(this.src)" ...
Andris