Javascript Form How to - Set text value to form input text control








Question

We would like to know how to set text value to form input text control.

Answer


<HTML>
<HEAD>
    <SCRIPT LANGUAGE="JavaScript">
    function checkText(){<!--   w ww  .j  a  va  2  s  .  c  o m-->
      if( document.FormExample3.textbox.value == "AAA"){
        console.log("You are correct!");
      } else {
        document.FormExample3.textbox.value = "AAA";
        console.log("That is incorrect. The correct answer is now in the text box.");
      }
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <FORM NAME="FormExample3" METHOD=POST>
    Input <INPUT TYPE="text" NAME="textbox" SIZE=25>
          <INPUT TYPE="BUTTON" NAME="Bugs" VALUE="Submit" onClick="checkText(this.form)">
    </FORM>
</BODY>
</HTML>

The code above is rendered as follows: