We have to assign one ID to each element of the page or the form we use. We can link up the elements by using this ID to handle any event or to manage its property.
innerHTML
<div id=my_msg></div>
<script type="text/javascript">
document.getElementById('my_msg').innerHTML=" Welcome to plus2net";
</script>
By using above code we can display the string inside the DIV tag. Output is here
Welcome to plus2net
value
<input type=text name=t1 id =t1 value='plus2net.com'>
<br>
<script type="text/javascript">
my_var=document.getElementById('t1');
document.write(my_var.value);// read the input data
</script>
We can set the default value to above input box
my_var.value='Welcome' // Assign value to input text box
We will learn how we can manage form components by using this ID tag. This way we need not keep any event handler like onfocus, onchange etc within the tag. A simple form can be written using these tags and all event handlers we can keep inside a function.
Demo of checkbox getElementById →
checkbox getElementById
While using a checkbox we can access the status of the checkbox or value of the checkbox by using getElementById. Here is some sample code.
<form name='form1' action='' method='post' onSubmit='return validate();'>
<input type=checkbox name=agree id='c1' value='yes-no'>
<input type=button onClick='return validate();' value=submit>
</form>
<p class='demo'><a href=form-id.php>Tutorial on form id</a></p>
<script language='JavaScript' type='text/JavaScript'>
<!--
function validate() {
if(document.getElementById('c1').checked) // True if checked
{
alert("Value : " + document.getElementById('c1').value); // display the value
alert("Name : " + document.getElementById('c1').name); // display the name
return true;
}
else{
alert("Checkbox is NOT checked ")
return false;
}
}
//-->
</script>
This type of forms are frequently used in various applications so we will try to keep one as reference by using all type of elements like textbox, checkbox, select list, radio button etc and learn how to handle them. To make our learning simple we will not keep the validation part here and those can be added as per requirements.
Let us display the form first and you can test this to see the messages it displays. We have written one alerts which will display the form element used and the text you typed or option you have selected. All the alerts are triggers by onchange event. Here is the form.
Demo of Form with ID →
The code to display the form is here. Note that there is no event handler within the form. Enter some data in this and see the alert box comes out.
The code for displaying the above form is here. Note that there is no event handler.
We have used window.onload function to initialize all form elements while the page loads. Then used the onchange event handler to trigger another function formValidation(). So Inside this function formValidation we are first reading the keyinput and which form element is used in various components. The two lines below first used inside the function formValidation collect the information about the element used.
Then the object properties of the tag is used to get details on tag id, tag value and tag name. All these information are kept in a variable msg and displayed through an alert window.
var msg="id = "+ txtField.id + " name =" +txtField.name + " value = "+txtField.value;
alert(msg);
The full code as explained above is displayed here. Note how differently the checkbox is handled.