I have a form:
<form method="post" name="Form1" action="default.asp" onsubmit="return processField();">
<input type="hidden" name="hiddentext1">
<input type="text" name="text1">
<input type="submit" value="Submit" id="button1">
</form>
And what I want is to call ProcessField
function on the submit of the form. I know that ProcessField
function works fine - tested it using an inline call. But now I want to attach the event via JavaScript. Below is my JavaScript code:
<script type="text/javascript">
if (window.addEventListener){
window.addEventListener('load', attachFormSubmit, false);
} else if (window.attachEvent){
window.attachEvent('onload', attachFormSubmit );
}
function attachFormSubmit(){
theForm = document.getElementById("Form1");
alert("attaching form submit");
if (theForm.addEventListener){
theForm.addEventListener('submit', CleanUpEID, false);
} else if (theForm.attachEvent){
theForm.attachEvent('onsubmit', CleanUpEID);
}
}
function ProcessField()
{
alert("processing field");
if (this.text1.value == '')
{
alert ("Please enter the value")
this.text1.focus()
return false
}
this.hiddentext1.value = this.text1.value;
// Disable button
with ($('button1')) {
value = "Processing ...";
disabled = true;
}
return true;
}
</script>
I have two issues with the above script:
it attaches the event to the form multiple times - every time page reloads. I suspect there is a better place for my code but cannot figure it out.
Keyword "this" in
processField
function comes up undefined. It works fine if I replace "this" with form name, but I was wondering what needs to be done in order for keyword "this" to work in this case.
I'd really appreciate if someone could point me in the right direction. Thanks.