I am learning Javascript with the material on the w3schools website and I cannot understand somehing in the explanation of the onsubmit event (to be found here)
I don't get why they use the
action="submit.htm"
in the form frm1 and why there isn't a simple html button that executes their greeting() function.
On top of this, I don't understand why in the alert box, their Javascript "command" is
document.forms["frm1"]["fname"].value
and not
document.frm1.fname.value
I read and re-read the code and searched but I cannot figure out answers to these (basic) questions.
Thank you in advance for any kind of explanation.
I don't get why they use the
action="submit.htm"
in the form frm1 and why there isn't a simple html button that executes their greeting() function.
The form action and the greeting function are two separate things. The form action tells where the form values are going to be submitted, so the sequence of events is 1) click the submit button, 2) greeting message pops up, 3) arrive at new page (submit.htm) with form values in HTTPPOST array for processing. OnSubmit is useful in, for example, client-side validation where you have to check that the form field values are valid before you allow them to be submitted.
I don't understand why in the alert box, their Javascript "command" is
document.forms["frm1"]["fname"].value
and not
document.frm1.fname.value
document.forms is an associative array or object that contains all of the form information on a page. Any specific form name is a key in this array. Another way to write it is:
document.forms.frm1.fname.value
I think you can actually do it your way but it isn't regular javascript syntax so it is avoided (especially by w3c):
Bookmarks