image type button not starting javascript action

Discussion in 'Javascript' started by Notgiven, Aug 7, 2006.

  1. Notgiven

    Notgiven Guest

    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!
    Notgiven, Aug 7, 2006
    #1
    1. Advertising

  2. Notgiven wrote:

    >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
    Michael A. Vickers, Aug 7, 2006
    #2
    1. Advertising

  3. Notgiven

    Randy Webb Guest

    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

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Aug 7, 2006
    #3
  4. Notgiven

    Randy Webb Guest

    Michael A. Vickers said the following on 8/7/2006 3:57 PM:
    > Notgiven wrote:
    >
    >> 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);"


    return(false)?

    return isn't a function, drop the parentheses:

    return false

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Aug 7, 2006
    #4
  5. Notgiven

    Notgiven Guest

    "Randy Webb" <> wrote in message
    news:...
    > 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
    >
    > --
    > 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!
    Notgiven, Aug 7, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Bloggs
    Replies:
    1
    Views:
    719
    Sudsy
    Aug 3, 2003
  2. rjweytens
    Replies:
    6
    Views:
    16,047
    rjweytens
    Jun 25, 2004
  3. runescience

    Struts mapping action to action???

    runescience, Feb 6, 2006, in forum: Java
    Replies:
    3
    Views:
    1,806
    runescience
    Feb 7, 2006
  4. vyshu
    Replies:
    1
    Views:
    2,523
    Richard Senior
    Apr 27, 2007
  5. Sam
    Replies:
    1
    Views:
    1,087
    Brian Kenneth Nielsen
    Nov 25, 2008
Loading...

Share This Page