Making link behave like submit button?

B

bill

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
 
M

Mick White

bill said:
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
 
L

Lee

Mick White said:
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.
 
M

Mick White

Lee said:
Mick White said:



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
 
L

Lee

Mick White said:
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"
 
M

Mick White

Lee said:
Mick White said:



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
 
G

Grant Wagner

Mick said:
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.
 
M

Mick White

Grant said:
Mick White wrote: :


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
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top