What Does Document. Form Mean in Javascript?

In JavaScript, what is the meaning of the identifiers document.cookie, document.forms and the .value field? I have trouble understanding the use of the below syntax example.

var x=document.forms["myForm"]["email"].value

Best wishes😀

3

3 Answers

document.forms["myForm"]["email"].value

that will get the value of the "email" element within the "myForm" <form>

<form id="myForm" name="myForm">
    <input id="email" name="email" value="" />
</form>

so x will equal ""


document.forms will return a collection of all of the forms within a particular page. writing document.forms["myForm"] will return the form with the name "myForm" from that collection

1

documents.forms is an object containing all of the forms for that HTML document. With this code, you are referencing the elements by their name attributes (not id). So this would provide a string containing the value for the form element with the name "email" within the form with the name "myForm".

Example:

<form name="contact-form">
Email: <input type="text" name="email" />
</form>

Executing the following JavaScript code at anytime when a value for the email field is desired would provide the value.

var contact_email = document.forms["contact-form"]["email"].value;

The contact_email variable would then contain the value entered into the input field.

this code shows how you can use document.forms in an example with validation.

``

        function validation(inputs){
            if (inputs==""|| inputs=="null"){
            alert("Enter Valid Number");
            return false;
        }
            if (isNaN(inputs)){
            alert("Enter Valid Number");
            return false;
        }
            return true;
        }


        function triNum(num){
            var triangle=0;
            for(i=1 ;i <= num; i++){
                triangle += i;
            }
            return triangle;
        }

        function squareNum(num){
            var square = num * num;
            return square;
        }


        function findNums(){
            //var num = document.getElementById('number1').value;
            var num= document.forms["MagicNum"]["FirstNum"].value;
            if (validation(num)){
                document.forms["MagicNum"]["tri"].value=triNum(num);
                document.forms["MagicNum"]["square"].value=squareNum(num);
            }
        }
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest