
form submits hrefs images and submit buttons
I just read this in Chapter 3 of Beginning JavaScript from WROX Press.
Apparently you can use an event handler to the <A> tag and use a return
value to stop or allow the HREF from firing the hyperlink.
I believe it is something like:
<SCRIPT Language=JavaScript>
function href_onclick(){
\\ do something the script may not be fully correct
document.thisform.submit; \\ submit the form.
return false; \\ stops the href firing.
return true; \\ allows the href to fire (may be redundant
after the .submit action).
}
</SCRIPT>
<A href="Somewhere.asp" onclick="JavaScript:return href_onclick();">Test
Hyperlink</A>
There is a possibility that NN (below version 4) browsers don't support the
HREF onclick() event (I think I read this this afternoon) but there is an
alternative: apparently JavaScrip can 'add' an event handler to the <A> tag
dynamically. Someone may be able to help with this. Failing that, have a
look at the WROX Press site for the download code for Chapter 3 / 4 of
'Beginning JavaScript'.
Chris Barber.
Quote:
> Try to change something below on "onsubmit" and "href".
> <form name=testform onsubmit="validForm(this)">
> <br><a class="Link1" href="javascript:document.testform.onsubmit();"
> >Search</a>
> hey all,
> is there any way that i configure an href="" to work the exact same way as
a
> submit button, or an image. I know how to make an href="" submit a form,
but
> it
> seems to work differently then an input type. In my example the go and the
> broken
> link image will both pop up with the requred fields, but the image wont.
Any
> help
> is appreciated.
> <table border="5" bordercolor="black">
> <tr><td>
> <script language="JavaScript">
> function validForm(testform)
> {
> var vInputArray = new Array('field2', 'field3')
> var vInputArrayNames = new Array('field 2', 'field 3')
> for (var i=0; i<vInputArray.length; i++)
> {
> if (testform[vInputArray[i]].value=="")
> {
> alert("You did not fill out the following information: " +
> vInputArrayNames[i])
> return false
> }
> }
> }
> </script>
> <form name=testform onsubmit="return validForm(this)">
> <br><input type="text" name="field1">field 1
> <br><input type="text" name="field2">field 2
> <br><input type="text" name="field3">field 3
> <br><input type="text" name="field4">field 4
> <br><input type="submit" value="go!">
> <br><a class="Link1" href="javascript:document.testform.submit();"
> onclick="javascript:return validForm(this)">Search</a>
> <br><input tabindex=14 align="absmiddle" src="submit.gif" type="image">
> </form>
> </td></tr>
> </table>