|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
How can a input submit call a function b4 submitting
Hi I am having problem modifying some feature of my proboard forum....well I want to first of all know how can I make an input button thats type of submit and already has something assigned to onClick call a little function I made in the header...
this is the code im facing... Code:
<script>
<!-- this is the function i put in the header to be called
function check_sublength() {
var allinputs = document.getElementsByTagName('input')
for (i=0; i<allinputs.length;i++) {
if (allinputs(i).name == "subject") {
var subjin = allinputs(i)
if (subjin.value == "") alert ("Empty subject field")
}
}
}
//-->
</script>
<!--This is in the footer( the bottom of the proboard code
<input type="hidden" name="waction" value="post">
<input type="submit" name="post" value="Post" onClick="document.postmodify.waction.value='post';" accesskey="s" tabindex="4">
Last edited by stumpy : February 14th, 2004 at 10:25 PM. Reason: Please place code in [CODE] tags |
|
#2
|
||||
|
||||
|
You can a few different things here.
One - if you want to assign a button to an event such as onClick - then use input type=button, not submit. Two, if you only want the function to run just before submitting, use the event method called onSubmit in the form tag. E.g. <form onSubmit="return checkLength()">. Notice the return statement. If your function returns a false, then the form will not be submitted. Obviosuly, a return true will submit the form. Check out this site - http://www.htmlgoodies.com/tutors/fm.html - It goes through the all the basics of forms and javascript. BTW - if your code is javascript, then your script tag should be written as follows: <script type="text/javascript"> |
|
#3
|
|||
|
|||
|
Thanx...but what if the form already has a value assigned to onSubmit...
Code:
<form action="index.cgi?board=computer&action=post2" method="post" name="postmodify" onSubmit="submitonce(this);"> can I add two values....how? |
|
#4
|
||||
|
||||
|
Create a new function that contains your function calls for the onSubmit event handler.
Code:
function onsubmit_functions() {
submitonce(document.frmName)
checkLength()
}
function submitone() {
...
}
functions checklength() {
...
}
<form onSubmit="onsubmit_functions()">
|
|
#5
|
|||
|
|||
|
I have tried to make the form call two functions. Everythings fine but why is the form still submitting to the location.....? I am returning false when needed but no response...what am i doing wrong?
<script type="text/javascript"> <!-- function check_post(){ var subjin; var rtn = true; var allinput = document.getElementsByTagName('input') for (i=0; i < allinput.length; i++){ if (allinput(i).name == 'subject'){ subjin = allinput(i); if (subjin.value == ""){ alert ("No Subject Specified") rtn = false; break; } } } return (rtn); } //--> </script> <script type="text/javascript"> <!--fag function submit_call(theform){ var rtn2 = check_post(); if (rtn2 == true){ if (document.all || document.getElementById) { for (i=0;i<theform.length;i++) { var tempobj=theform.elements[i]; if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset") { tempobj.disabled=true; } } } } else { rtn2 = false; } return (rtn2); } //--> </script> <form action="index.cgi?board=announcements&action=post2" method="post" name="postmodify" onSubmit="submit_call(this);"> |
![]() |
| Viewing: Dev Articles Community Forums > Programming > JavaScript Development > How can a input submit call a function b4 submitting |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|