Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Making link behave like submit button?

Reply
Thread Tools

Making link behave like submit button?

 
 
bill
Guest
Posts: n/a
 
      07-07-2004




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

 
Reply With Quote
 
 
 
 
Mick White
Guest
Posts: n/a
 
      07-07-2004
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
 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      07-07-2004
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.formNam e))
>{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.

 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      07-07-2004
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.formNa me))
>>{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
>

 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      07-07-2004
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.formN ame))
>>>{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.fo rmName.submit()};return false"

 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      07-07-2004
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.fo rmName.submit()};return false"
>


I thought that a return "false" might cancel the submission, of course I
would probably be wrong.
Thanks, Lee.
Mick
 
Reply With Quote
 
Grant Wagner
Guest
Posts: n/a
 
      07-07-2004
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.fo rmName.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


 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      07-07-2004
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
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making html button behave like hyperlink WJ HTML 11 03-03-2011 11:27 AM
Cisco switch behave like an hub Moti.Ba@gmail.com Cisco 6 02-26-2010 11:35 AM
how to prevent a page refresh to behave like a submit postback? Carlos ASP .Net 2 07-02-2008 02:20 PM
How to convert a fast ethernet interface in a Cisco Router to behave like a serial interface Moloy Cisco 1 03-18-2006 02:16 PM
LINK, ALINK, VLINK colours don't behave Peter Charles HTML 21 08-16-2004 02:19 PM



Advertisments