Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass form as argument into its submit event handler

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
What I'm trying to do should be very simple. I'd like to assign a submit handler to a form and pass that form into the handler (I then wish to interrogate the id of the form argument and perform appropriate validation according to which form it is). The following code doesn't achieve the desired result. Inside the canSubmit function, according to firebug, the form argument seems to be of type form#[object HTMLInputElement] and form.id is undefined. As you can probably see I'm trying to implement this unobtrusively i.e. with behaviour separated into a separate js file. For information, I'm using jquery.
Code:
// in domready event handler
$('form').submit(function() { return canSubmit(this); });
Code:
function canSubmit(form) {
  switch(form.id) {
    case "enhancementForm":
    ...
    case "newitemForm":
    ...
  }
  return true;

}
Any pointers would be much appreciated.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
my test code here (in IE 7), it running good
Code:
<script type="text/javascript" src="jquery.js"></script>
   <form name="form1" id="form1">
		<input type="submit" value="gaga" />
	</form>
	<form name="form2"  id="form2">
		<input type="submit" value="gaga" />
	</form>
	<script>
		function canSubmit(form) {
		  switch(form.id) {
			case "form1":
				alert("form1"); break;
			case "form2":
				alert("form2");break;
		  }
		  return false;
		}
         // in domready event handler
		$('form').submit(function() { return canSubmit(this);});
 
Thanks for the input sanshizi. I eventually found that the problem was nothing to do with the code I posted. I'd unwittingly set the id and name attributes of one of my form elements to "id". So, when I was checking form.id it was returning an object representing the form element rather than giving me the id of the form!

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top