image type button not starting javascript action

N

Notgiven

This works:
<input type="button" value="Add to list" onClick="addOption(param1,
param2);">

But this doesn't:
<img src="images/add.gif" onClick="addOption(param1, param2);">

The error I get using the img tag is, "this.form has no properties". Yet it
is right beside the input tag that does work with the same OnClick= params

This doesn't work either:
<input type="image" src="images/add.gif" value="Add to list"
onClick="addOption(param1, param2);">

The error I get with this is that it does the javascript action then Submits
the form - I do not want it to submit, just do the javascript action.

What can I do to get a graphic button that will complete the javascript
action?

Thanks!
 
M

Michael A. Vickers

Notgiven said:
This doesn't work either:
<input type="image" src="images/add.gif" value="Add to list" onClick="addOption(param1, param2);"

The error I get with this is that it does the javascript action then Submits
the form - I do not want it to submit, just do the javascript action.

try

onClick="addOption(param1, param2);return(false);"


Michael
 
R

Randy Webb

Notgiven said the following on 8/7/2006 3:50 PM:
This works:
<input type="button" value="Add to list" onClick="addOption(param1,
param2);">

But this doesn't:
<img src="images/add.gif" onClick="addOption(param1, param2);">

The error I get using the img tag is, "this.form has no properties". Yet it
is right beside the input tag that does work with the same OnClick= params

That is because img elements are not part of the form so when you try to
refer to this.form you get the error.
This doesn't work either:
<input type="image" src="images/add.gif" value="Add to list"
onClick="addOption(param1, param2);">

The error I get with this is that it does the javascript action then Submits
the form - I do not want it to submit, just do the javascript action.

That is because the default behavior for input type="image" is to submit
the form.
What can I do to get a graphic button that will complete the javascript
action?

Pass a reference to your form to the function.

function addOption(formRef,param,param2){

}

onclick="addOption('document.myForm',param1,param2)"

Or something similar. Without seeing your function it is *impossible* to
give a definitive answer
 
R

Randy Webb

Michael A. Vickers said the following on 8/7/2006 3:57 PM:
try

onClick="addOption(param1, param2);return(false);"

return(false)?

return isn't a function, drop the parentheses:

return false
 
N

Notgiven

Randy Webb said:
Notgiven said the following on 8/7/2006 3:50 PM:

That is because img elements are not part of the form so when you try to
refer to this.form you get the error.


That is because the default behavior for input type="image" is to submit
the form.


Pass a reference to your form to the function.

function addOption(formRef,param,param2){

}

onclick="addOption('document.myForm',param1,param2)"

Or something similar. Without seeing your function it is *impossible* to
give a definitive answer

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/

Randy - that worked perfectly - many thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top