Making link behave like submit button?

Discussion in 'Javascript' started by bill, Jul 7, 2004.

  1. bill

    bill Guest

    Is there any way to make a link (as marked by an <A>-tag, that is),
    behave exactly like a submit button?

    Many thanks!

    -bill
     
    bill, Jul 7, 2004
    #1
    1. Advertising

  2. bill

    Mick White Guest

    bill wrote:

    > Is there any way to make a link (as marked by an <A>-tag, that is),
    > behave exactly like a submit button?
    >
    > Many thanks!
    >
    > -bill
    >

    Yes, but only if you're sure that js is enabled.
    <a href="NoJSPage.html"
    onclick="this.href='#';document.formName.submit();">Click</a>

    If you have a form verification function:
    <a href="NoJSPage.html"
    onclick="this.href='#';if(verify(document.formName))
    {document.formName.submit();}">Click</a>

    http://www.mickweb.com/demo/formVerification.html

    Mick
     
    Mick White, Jul 7, 2004
    #2
    1. Advertising

  3. bill

    Lee Guest

    Mick White said:
    >
    >bill wrote:
    >
    >> Is there any way to make a link (as marked by an <A>-tag, that is),
    >> behave exactly like a submit button?
    >>
    >> Many thanks!
    >>
    >> -bill
    >>

    > Yes, but only if you're sure that js is enabled.
    ><a href="NoJSPage.html"
    >onclick="this.href='#';document.formName.submit();">Click</a>
    >
    >If you have a form verification function:
    ><a href="NoJSPage.html"
    >onclick="this.href='#';if(verify(document.formName))
    >{document.formName.submit();}">Click</a>


    Rather than setting the href value in the onclick handler,
    simply have the onclick handler return false. That will
    prevent the link from being followed. Failing to return
    false may cause the submit to fail, since the browser is
    being told to load the page specified by the action and
    to reload the current page (href="#"), simultaneously.
     
    Lee, Jul 7, 2004
    #3
  4. bill

    Mick White Guest

    Lee wrote:

    > Mick White said:
    >
    >>bill wrote:
    >>
    >>
    >>>Is there any way to make a link (as marked by an <A>-tag, that is),
    >>>behave exactly like a submit button?
    >>>
    >>>Many thanks!
    >>>
    >>> -bill
    >>>

    >>
    >> Yes, but only if you're sure that js is enabled.
    >><a href="NoJSPage.html"
    >>onclick="this.href='#';document.formName.submit();">Click</a>
    >>
    >>If you have a form verification function:
    >><a href="NoJSPage.html"
    >>onclick="this.href='#';if(verify(document.formName))
    >>{document.formName.submit();}">Click</a>

    >
    >
    > Rather than setting the href value in the onclick handler,
    > simply have the onclick handler return false. That will
    > prevent the link from being followed. Failing to return
    > false may cause the submit to fail, since the browser is
    > being told to load the page specified by the action and
    > to reload the current page (href="#"), simultaneously.


    Without this.href="#", a return of "true" might load the non js page, no?
    Mick
    >
     
    Mick White, Jul 7, 2004
    #4
  5. bill

    Lee Guest

    Mick White said:
    >
    >Lee wrote:
    >
    >> Mick White said:
    >>
    >>>bill wrote:
    >>>
    >>>
    >>>>Is there any way to make a link (as marked by an <A>-tag, that is),
    >>>>behave exactly like a submit button?
    >>>>
    >>>>Many thanks!
    >>>>
    >>>> -bill
    >>>>
    >>>
    >>> Yes, but only if you're sure that js is enabled.
    >>><a href="NoJSPage.html"
    >>>onclick="this.href='#';document.formName.submit();">Click</a>
    >>>
    >>>If you have a form verification function:
    >>><a href="NoJSPage.html"
    >>>onclick="this.href='#';if(verify(document.formName))
    >>>{document.formName.submit();}">Click</a>

    >>
    >>
    >> Rather than setting the href value in the onclick handler,
    >> simply have the onclick handler return false. That will
    >> prevent the link from being followed. Failing to return
    >> false may cause the submit to fail, since the browser is
    >> being told to load the page specified by the action and
    >> to reload the current page (href="#"), simultaneously.

    >
    >Without this.href="#", a return of "true" might load the non js page, no?


    Sure, so you simply return false:

    onclick="if(verify(document.formName)){document.formName.submit()};return false"
     
    Lee, Jul 7, 2004
    #5
  6. bill

    Mick White Guest

    Lee wrote:

    > Mick White said:
    >
    >>Lee wrote:


    >>
    >>Without this.href="#", a return of "true" might load the non js page, no?

    >
    >
    > Sure, so you simply return false:
    >
    > onclick="if(verify(document.formName)){document.formName.submit()};return false"
    >


    I thought that a return "false" might cancel the submission, of course I
    would probably be wrong.
    Thanks, Lee.
    Mick
     
    Mick White, Jul 7, 2004
    #6
  7. bill

    Grant Wagner Guest

    Mick White wrote:

    > Lee wrote:
    >
    > > Mick White said:
    > >
    > >>Lee wrote:

    >
    > >>
    > >>Without this.href="#", a return of "true" might load the non js page, no?

    > >
    > >
    > > Sure, so you simply return false:
    > >
    > > onclick="if(verify(document.formName)){document.formName.submit()};return false"
    > >

    >
    > I thought that a return "false" might cancel the submission, of course I
    > would probably be wrong.
    > Thanks, Lee.
    > Mick


    Returning false from any event handler should, and usually does, stop the element's
    default behaviour that triggered the event. It is not specifically related to forms.
    Examples for "return false;":

    Returning false to the onsubmit event of a <form ...> prevents form submission because
    the submission of the form was the triggered event.

    Returning false to the onclick event of an <input type="submit" ...> prevents form
    submission, but only because it stops the "click" of the submit button, which prevents
    the submission.

    Returning false to the onclick event of an <a ...> stops the "click" of the link, so
    the href attribute never gets followed.

    Returning false to the onkeydown event of <input type="text" ...> prevents the key
    that was pressed down from appearing in the input.

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
     
    Grant Wagner, Jul 7, 2004
    #7
  8. bill

    Mick White Guest

    Grant Wagner wrote:

    > Mick White wrote:

    :
    >>>
    >>>onclick="if(verify(document.formName)){document.formName.submit()};return false"
    >>>

    >>
    >>I thought that a return "false" might cancel the submission, of course I
    >>would probably be wrong.
    >>Thanks, Lee.
    >>Mick

    >
    >
    > Returning false from any event handler should, and usually does, stop the element's
    > default behaviour that triggered the event. It is not specifically related to forms.
    > Examples for "return false;":
    >
    > Returning false to the onsubmit event of a <form ...> prevents form submission because
    > the submission of the form was the triggered event.
    >
    > Returning false to the onclick event of an <input type="submit" ...> prevents form
    > submission, but only because it stops the "click" of the submit button, which prevents
    > the submission.


    If I remove [this.href="#" ], and add "return false", the original link
    is followed, without form submission (as you suggest).
    The form works as it should @
    http://www.mickweb.com/demo/formVerification.html
    Try it.
    Mick

    >
    > Returning false to the onclick event of an <a ...> stops the "click" of the link, so
    > the href attribute never gets followed.
    >
    > Returning false to the onkeydown event of <input type="text" ...> prevents the key
    > that was pressed down from appearing in the input.
    >
    > --
    > Grant Wagner <>
    > comp.lang.javascript FAQ - http://jibbering.com/faq
    >
    >
     
    Mick White, Jul 7, 2004
    #8
    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. Russell
    Replies:
    3
    Views:
    6,925
    Lau Lei Cheong
    Jun 24, 2004
  2. C.F.
    Replies:
    0
    Views:
    643
  3. Peter Charles

    LINK, ALINK, VLINK colours don't behave

    Peter Charles, Aug 15, 2004, in forum: HTML
    Replies:
    21
    Views:
    1,832
    Dan Abrey
    Aug 16, 2004
  4. WJ
    Replies:
    11
    Views:
    136,029
    adallas
    Mar 3, 2011
  5. Carlos
    Replies:
    2
    Views:
    828
    Carlos
    Jul 2, 2008
Loading...

Share This Page