{"id":155,"date":"2018-07-26T10:58:35","date_gmt":"2018-07-26T10:58:35","guid":{"rendered":"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=155"},"modified":"2018-08-02T06:45:40","modified_gmt":"2018-08-02T06:45:40","slug":"javascript-and-event-handling","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/chapter\/javascript-and-event-handling\/","title":{"rendered":"JavaScript and Event Handling"},"content":{"raw":"<div>\r\n\r\n<strong>JavaScript - Event Handlers<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">JavaScript's interaction with HTML is handled through events that occur when the user or the browser manipulates a page. When the page loads, it is called an event. It allows us to execute an action of code when the event occurs. All the event handlers in JavaScript start with the keyword <strong><em>on<\/em><\/strong>, and each event handler deals with a certain type of event. (For eg, <strong>onClick<\/strong> )<\/p>\r\n&nbsp;\r\n\r\n<strong>How it works:<\/strong>\r\n\r\n&nbsp;\r\n\r\nStep 1:\u00a0 \u00a0 User moves mouse over object\r\n\r\nStep 2:\u00a0 \u00a0Event senses that something happened to the object\r\n\r\nStep 3:\u00a0 \u00a0JavaScript (Event handler) tells the object what to do\r\n\r\nStep 4:\u00a0 \u00a0Using DOM, the handler locates the object on the web page\r\n\r\nStep 5:\u00a0 \u00a0Object\u2019s image source is changed\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\">Here Step is an action where the user moves the mouse over an object. This action is sensed as an event, and JavaScript handles this event with an event handler. Using DOM, the object is located and the reaction to the event is performed as toggling the source image to anaother one. This is shown in the below diagram.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-156 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-83.png\" alt=\"\" width=\"669\" height=\"85\" \/><\/p>\r\n<p style=\"text-align: justify\">The following is the list of event handlers described along with the Objects on which they can be applied and when they are triggered as given in Table 19.1.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>TABLE 19.1: Event Handlers<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-158 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-85.png\" alt=\"\" width=\"510\" height=\"918\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n\r\nNow let us demonstrate some of the event type with examples and the output of it.\r\n\r\n&nbsp;\r\n\r\n<strong>onClick Event Type<\/strong>\r\n\r\n&nbsp;\r\n\r\nThis event occurs when a user clicks the left button of his mouse.\r\n\r\n&nbsp;\r\n\r\n<strong>Example 1<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt; script type=\"text\/javascript\"&gt;\r\n\r\nfunction sayHello()\r\n\r\n{\r\n\r\nalert(\"Hello World\")\r\n\r\n}\r\n\r\n&lt;\/script&gt;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/head&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;body&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;p&gt;Click the following button and see result&lt;\/p&gt; &lt;form&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;input type=\"button\" onclick=\"sayHello()\" value=\"Say Hello\" \/&gt; &lt;\/form&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\">\u00a0 On clicking the button it calls the function sayHello() which alerts a message as shown in FIgure 19.1 and Figure 19.2.<\/p>\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-159 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-86.png\" alt=\"\" width=\"291\" height=\"357\" \/><\/p>\r\n<strong>Example 2<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;title&gt; color world &lt;\/title&gt;\r\n\r\n&lt;script language = \"JAVASCRIPT\"&gt;\r\n\r\nfunction color(f, a)\r\n\r\n{\r\n\r\ndocument.bgColor=a;\r\n\r\nalert(a);\r\n\r\n}\r\n\r\n&lt;\/script&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body bgcolor=gray&gt;\r\n\r\n&lt;form name=myform&gt;\r\n\r\n&lt;input type = button value=red onClick=color(this.form,\"RED\")&gt;\r\n\r\n&lt;input type = button value=orange onClick=color(this.form,\"ORANGE\")&gt; &lt;input type = button value=green onClick=color(this.form,\"GREEN\")&gt; &lt;input type = button value=violet onClick=color(this.form,\"VIOLET\")&gt; &lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This example is to demonstrate the onClick event type which changes the background color of the document on clicking the respective buttons as shown in Figure 19.3 and Figure 19.4.<\/p>\r\n&nbsp;\r\n\r\n<strong>Output<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-160 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-87.png\" alt=\"\" width=\"477\" height=\"285\" \/><\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-161 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-88.png\" alt=\"\" width=\"506\" height=\"301\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0onSubmit Event type<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe <strong>onSubmit<\/strong> is an event that occurs when you try to submit a form.\r\n\r\n<strong>Syntax<\/strong>\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;script type=\"text\/javascript\"&gt;\r\n\r\nfunction validate()\r\n\r\n{\r\n\r\nall validation goes here\r\n\r\n.........\r\n\r\nreturn either true or false\r\n\r\n}\r\n\r\n&lt;\/script&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form method=\"POST\" action=\"t.cgi\" onsubmit=\"return validate()\"&gt; &lt;input type=\"submit\" value=\"Submit\" \/&gt;\r\n\r\n&lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n&nbsp;\r\n\r\nThe example demonstates that the form is validated when a submit button is clicked as shown in Figure 19.5.\r\n\r\n<\/div>\r\n<strong>\u00a0 \u00a0 Output:<\/strong>\r\n\r\n<img class=\"size-full wp-image-164 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-91.png\" alt=\"\" width=\"253\" height=\"127\" \/>\r\n\r\n<strong>onMouseOver and onMouseOut Event<\/strong>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\nThe <strong>onMouseOver<\/strong> event is triggered when you bring your mouse over any element on the document and the <strong>onMouseOut<\/strong> event is triggered when you move your mouse out from that element.\r\n\r\n&nbsp;\r\n\r\n<strong>Example 1:<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;script type=\"text\/javascript\"&gt;\r\n\r\nfunction over() {\r\n\r\ndocument.write (\"Mouse Over\");\r\n\r\n}\r\n\r\nfunction out() {\r\n\r\ndocument.write (\"Mouse Out\");\r\n\r\n}\r\n\r\n&lt;\/script&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;p&gt;Bring your mouse inside the division to see the result:&lt;\/p&gt;\r\n\r\n&lt;div onmouseover = \"over()\" onmouseout= \"out()\u201c &gt; &lt;h2&gt; This is inside the division &lt;\/h2&gt;\r\n\r\n&lt;\/div&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This example demonstrates the onmouseover and onmouseout event types. When the mouse is moved over the text, the function over() is called which displays the message \"Mouse Over\" and when the mouse is moved out of the text , the function out() is called which displays the message \"Mouse Out\". The output of this example code is shown in the Figure 19.6 and Figure 19.7.<\/p>\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-165 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-92.png\" alt=\"\" width=\"301\" height=\"278\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Example 2:<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;title&gt; image world &lt;\/title&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body &gt;\r\n\r\n&lt;a href=ex.html onMouseOver = document.images[0].src = \"FLOWER.jpg\"\r\n\r\nonMouseOut = document.images[0].src = \"GRAPES.jpg\" &gt; &lt;img src=\"GRAPES.jpg\" wi dth=160 height=100 border=1&gt; &lt;\/a&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This example toggles the image on thedocument when the mouse is moved over or moved out of the image displayed. The output of this example code is shown in Figure 19.8 and Figure 19.9.<\/p>\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-166 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-93.png\" alt=\"\" width=\"439\" height=\"512\" \/><\/p>\r\n<p style=\"text-align: center\"><strong>Figure 19.9<\/strong><\/p>\r\n&nbsp;\r\n<div>\r\n\r\n<strong>JavaScript Form Validation<\/strong>\r\n\r\n&nbsp;\r\n\r\nHTML form validation can be done by writing a script code using JavaScript.\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 <\/strong>\r\n\r\n<strong>Example:<\/strong>\r\n\r\n&nbsp;\r\n\r\nAn example to validate the name to be filled out without leaving it blank is shown below.\r\n\r\nfunction validateForm() {\r\n\r\nvar x = document.forms[\"myForm\"][\"fname\"].value; if (x == null || x == \"\") {\r\n\r\nalert(\"Name must be filled out\");\r\n\r\nreturn false;\r\n\r\n}\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n<strong>JavaScript Forms<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;form name=\"myForm\" action=\"demo_form.asp\" <strong>onsubmit=\"return<\/strong> <strong>validateForm()\" <\/strong>method=\"post\"&gt;\r\n\r\nName: &lt;input type=\"text\" name=\"fname\"&gt;\r\n\r\n&lt;input type=\"submit\" value=\"Submit\"&gt;\r\n\r\n&lt;\/form&gt;\r\n\r\n&nbsp;\r\n\r\n<strong>Example 1:<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;script type=\"text\/javascript\"&gt;\r\n\r\nfunction newValue()\r\n\r\n{\r\n\r\nvar x=document.forms.myForm\r\n\r\nx[0].value=\"The mere act of aiming at something makes you big\"\r\n\r\nx[0].select()\r\n\r\n}\r\n\r\n&lt;\/script&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form name=\"myForm\"&gt;\r\n\r\n&lt;textarea name=\"myTextarea\" rows=\"10\" cols=\"20\"&gt; The roots of e ducation are bitter but the fruit is sweeter &lt;\/textarea&gt;&lt;br \/&gt;\r\n\r\n&lt;input type=\"button\" onclick=\"newValue()\" value=\"New Value\"&gt; &lt;\/form&gt;&lt;\/body&gt;&lt;\/html&gt;\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\">This example is to demonstrate about validating a form which has a textarea. The textarea is initially shown with the text. On clicking the button, the function newValue() is called which displays another text. The method select() is used to display the textarea as selected. The output is shown in FIgure 19.10.<\/p>\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-167 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-94.png\" alt=\"\" width=\"608\" height=\"393\" \/><\/p>\r\n\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0Example 2:<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;title&gt; Forms&lt;\/title&gt;\r\n\r\n&lt;!--This code checks the Checkbox When the button is clicked --&gt;\r\n\r\n&lt;script Language='JavaScript'&gt;\r\n\r\nfunction Chk(f1)\r\n\r\n{\r\n\r\nf1.Check.checked=true;\r\n\r\nwindow.alert(\"The Checkbox just got checked\");\r\n\r\nf1.Check.checked=false;\r\n\r\nf1.Radio[0].checked=true;\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">f1.Radio[1].checked=false;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">window.alert(\"The Radio button just got checked\");<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/script&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/head&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;body&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;form&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Client Name :\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Input Type =Text Name=\"Text\" Value=\"\"&gt;&lt;br&gt;&lt;br&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">Client Address : &lt;Input Type =Text Name=\"Text1\" Value=\"\"&gt;&lt;br&gt;&lt;br&gt; Client E-mail Address : &lt;Input Type =Text Name=\"Text2\" Value=\"\"&gt;&lt;br&gt;&lt;br&gt; &lt;Input Type=\"radio\" Name=\"Radio\" Value=\"\"&gt; Male<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;Input Type=\"radio\" Name=\"Radio\" Value=\"\"&gt; Female&lt;br&gt;&lt;br&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;Input Type=\u201ccheckbox\" Name=\"Check\" Value=\"\"&gt; Em ploye d &lt;br&gt;&lt;br&gt; &lt;Input Type=\"Button\" Name=\"Bt\" Value=\"Set Element Array Value\"<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">onClick=\"Chk(this.form)\"&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/form&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span>\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\">This example is to demonstrate about validating a form which has a checkbox or a radiobutton. If checkbox is checked or radiobutton is checked, the property \"checked\" is made true and it will display the message as checked. The output is shown in Figure 19.11.<\/p>\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-168 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-95.png\" alt=\"\" width=\"584\" height=\"348\" \/><\/p>\r\n&nbsp;\r\n\r\n<strong>Example 3:<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;script language=\"javascript\"&gt;\r\n\r\n&lt;\/script&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form&gt;\r\n\r\nFirstName:&lt;input type=\"text\" name=\"Firstname\" size=20 onFocus=\"this.blur();\"&gt;\r\n\r\nLastName:&lt;input type=\"text\" name=\"Lastname\" size=20&gt;&lt;p&gt;\r\n\r\nAddress:&lt;input type=\"text\" name=\"Address\" size=60&gt;&lt;p&gt; pincode:&lt;input type=\"text\" name=\"Pincode\" size=6&gt;&lt;p&gt; &lt;input type=\"button\" name=\"act\" value=\"verify\"&gt; &lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This example code demonstrates the use of function onFocus on a textfield. When a focus is gained on the textfield, it is made to blur using the function blur() which will not allow to enter any data in the textfield. The output is shown in Figure 19.12.<\/p>\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-169 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-96.png\" alt=\"\" width=\"587\" height=\"347\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n\r\n<strong>Example 4:<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;title&gt;forms1&lt;\/title&gt;\r\n\r\n&lt;script&gt;\r\n\r\nfunction func(f1)\r\n\r\n{\r\n\r\nalert(\"the form elements have been cleared\");\r\n\r\n}\r\n\r\n&lt;\/script&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form onReset=\"func(this.form)\"&gt;\r\n\r\nClient Name: &lt;input type=\"text\" name=\"text\" value=\"\"&gt;&lt;br&gt;&lt;br&gt;\r\n\r\nClient address: &lt;input type=\"text\" name=\"text1\" value=\"\"&gt;&lt;br&gt;&lt;br&gt; &lt;input type=\"radio\" name=\"radio\" value=\"\"&gt;male\r\n\r\n&lt;input type=\"radio\" name=\"radio\" value=\"\"&gt;female&lt;br&gt;&lt;br&gt;\r\n\r\n&lt;input type=\"checkbox\" name=\"check\" value=\"\"&gt;employed&lt;br&gt;&lt;br&gt; &lt;input type=\"reset\" name=\"rst\" value=\"reset\"&gt;\r\n\r\n&lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This example code demonstrates validating a form on a reset button. The event onReset is triggered when a reset button is clicked which clears the form content and displays the message as cleared. The output is shown in Figure 19.13<\/p>\r\n\r\n<\/div>\r\n<strong>\u00a0 \u00a0 Output:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-170 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-97.png\" alt=\"\" width=\"574\" height=\"325\" \/><\/p>\r\n\r\n<div>\r\n\r\n<strong>Example 5:<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;title&gt; using text and button objects&lt;\/title&gt;\r\n\r\n&lt;script language=\"javascript\"&gt;\r\n\r\nfunction calculate(form)\r\n\r\n{\r\n\r\nform.results.value= eval(form.entry.value * 10 );\r\n\r\n}\r\n\r\n&lt;\/script&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form&gt;\r\n\r\n&lt;input type=\"text\" name=\"entry\" value=\"\"&gt;\r\n\r\n&lt;input type=\"button\" value=\"calculate\" onClick=\"calculate(this.form);\"\r\n\r\n&lt;br&gt;\r\n\r\n&lt;input type=\"text\" name=\"results\" onFocus=\"this.blur();\"&gt; &lt;br&gt;\r\n\r\n&lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n<\/div>\r\n<p style=\"text-align: justify\">This example code demonstrates the use of onClick() and onFocus(). The onFocus is applied on results textfield which will not allow to enter any value using the blur() method. The value entered on the entry field is calculated and shown in the result field. The output of this code is shown in Figure 19.14.<\/p>\r\n&nbsp;\r\n\r\n<strong>Output:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-171 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-98.png\" alt=\"\" width=\"603\" height=\"376\" \/><\/p>\r\n&nbsp;\r\n<div>\r\n\r\n<strong>Example 6:<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;htMl&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;title&gt;working with checkboxes&lt;\/title&gt;\r\n\r\n&lt;script&gt;\r\n\r\nfunction calculate(form, callingField)\r\n\r\n{\r\n\r\nif(callingField==\"result\")\r\n\r\n{\r\n\r\nif(form.square.checked)\r\n\r\n{\r\n\r\nform.entry.value= Math.sqrt(form.result.value);\r\n\r\n}\r\n\r\nelse\r\n\r\n{\r\n\r\nform.entry.value=form.result.value\/2;\r\n\r\n}\r\n\r\n}\r\n\r\n<span style=\"font-size: 1em;text-align: initial\">else {<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">if(form.square.checked)<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">{<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">form.result.value = form.entry.value * form.entry.value;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">E<\/span><span style=\"text-align: initial;font-size: 1em\">lse {<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">form.result.value = form.entry.value*2;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">}}<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/script&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/head&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;body&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;form&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;center&gt;&lt;br&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;b&gt; value: &lt;\/b&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;input type =\"text\" name = \"entry\" value = 0 onChange =\"calculate(this.form,this.name);\"&gt; &lt;br&gt;&lt;br&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;b&gt;action&lt;\/b&gt;(default-double):<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;input type =\"checkbox\" name = \"square\" onClick = \"calculate(this.form,this.name);\"&gt;square &lt;br&gt;&lt;br&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;b&gt; result: &lt;\/b&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;input type =\"text\" name = \"result\" value = 0 onChange = \"calculate(this.form,this.name);\"&gt; &lt;\/center&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/form&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">This example code demonstrates the use of onClick and onChange event types. There are two textfields (entry and result) and a radiobutton in the form. If the calling field is result field, and if the checkbox is checked then it calculates the squareroot of the value entered in the result field and displays in the entry field else it calculates half of the result value and displays in the entry field. Similarly when the value entered in the entry field changes, based on the checkbox checked or not checked displays the square of the value entered in the entry field or multiplies the value by 2 and displays in the result field. The output of this code is shown in Figure 19.15.<\/p>\r\n\r\n<\/div>\r\n<strong>\u00a0 \u00a0 Output:<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><img class=\"size-full wp-image-172 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-99.png\" alt=\"\" width=\"609\" height=\"384\" \/><\/p>\r\n\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0JavaScript Cookies<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A cookie is used to store information on the user\u2019s computer even when the user switches off his\/her computer. It is a data that is sent from a web server to a web browser when the user visits a site on a server. It is just a .txt file stored in a user\u2019s computer. A cookie can be associated with one or more documents on the web server. More than one cookie can be associated with a document on the web server. Every cookie has a NAME-VALUE pair associated with it. Cookies have an expiration date associated with them.<\/p>\r\n&nbsp;\r\n\r\nCookies are a plain text data record of 5 variable-length fields:\r\n\r\n&nbsp;\r\n\r\n1.<strong>Expires <\/strong>\u2212 The date the cookie will expire. If this is blank, the cookie will expire when the visitor quits the browser.\r\n\r\n2.<strong>Domain <\/strong>-The domain name of your site.\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">3.\u00a0<\/span><strong style=\"text-align: initial;font-size: 1em\">Path <\/strong><span style=\"text-align: initial;font-size: 1em\">\u2212 The path to the directory or web page that set the cookie. This may be blank if you want to retrieve the cookie from any directory or page.<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">4.<\/span><strong style=\"text-align: initial;font-size: 1em\">Secure <\/strong><span style=\"text-align: initial;font-size: 1em\">\u2212 If this field contains the word \"secure\", then the cookie may only be retrieved with a secure server. If this field is blank, no such restriction exists.<\/span>\r\n\r\n<span style=\"text-align: initial;font-size: 1em\">5.\u00a0<\/span><strong style=\"text-align: initial;font-size: 1em\">Name-Value <\/strong><span style=\"text-align: initial;font-size: 1em\">- Cookies are set and retrieved in the form of key-value pairs.<\/span>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 \u00a0Create Cookies<\/strong>\r\n\r\n&nbsp;\r\n\r\nWe can create a cookie by assigning a string value to the document.cookie object.document.cookie = \"key1=value1;key2=value2;expires=date\";\r\n\r\n&nbsp;\r\n\r\n<strong>Store Cookies<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Example<\/strong>\r\n\r\n&nbsp;\r\n\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;script type=\"text\/javascript\"&gt;\r\n\r\nfunction WriteCookie()\r\n\r\n{\r\n\r\nif( document.myform.customer.value == \"\" )\r\n\r\n{\r\n\r\nalert(\"Enter some value!\");\r\n\r\nreturn;\r\n\r\n}\r\n\r\ncookievalue= escape(document.myform.customer.value) + \";\"; document.cookie=\"name=\" + cookievalue;\r\n\r\ndocument.write (\"Setting Cookies : \" + \"name=\" + cookievalue );\r\n\r\n}\r\n\r\n&lt;\/script&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form name=\"myform\" action=\"\"&gt;\r\n\r\nEnter name: &lt;input type=\"text\" name=\"customer\"\/&gt;\r\n\r\n&lt;input type=\"button\" value=\"Set Cookie\" onclick=\"WriteCookie();\"\/&gt; &lt;\/form&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The example shows how a cookie can be created and stored. It retrieves the value from the textfield and stores it as a cookie.<\/p>\r\n\r\n<\/div>\r\n&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;script type=\"text\/javascript\"&gt;\r\n\r\nfunction ReadCookie()\r\n\r\n{\r\n\r\nvar allcookies = document.cookie;\r\n\r\ndocument.write (\"All Cookies : \" + allcookies );\r\n<ul>\r\n \t<li>\/\/ Get all the cookies pairs in an array cookiearray = allcookies.split(';');<\/li>\r\n \t<li>\/\/ Now take key value pair out of this array for(var i=0; i&lt;cookiearray.length; i++)<\/li>\r\n<\/ul>\r\n{\r\n\r\nname = cookiearray[i].split('=')[0]; value = cookiearray[i].split('=')[1];\r\n\r\ndocument.write (\"Key is : \" + name + \" and Value is : \" + value);\r\n\r\n}\r\n\r\n}\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The function ReadCookie() retrieve the stored cookie from document.cookie and store in a variable.Then we can use a for loop to read each cookie as a key-value pair.<\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">This module explains about the JavaScript Event handling mechanisms. This module also explores about working with HTML form validation using HTML DOM and Event handling and finally discusses about JavaScript Cookies.<\/p>\r\n&nbsp;\r\n\r\n<strong>Web Links<\/strong>\r\n<ul>\r\n \t<li><em>http:\/\/www.w3schools.com\/<\/em><\/li>\r\n \t<li><em>www.doc.ic.ac.uk\/~rcheung\/teaching\/2720\/ppt\/lecture04c.ppt<\/em><\/li>\r\n \t<li><em>Paul J. Deitel, Harvey M. Deitel, Abbey Deitel, \"Internet and World Wide Web How To Program\", 5th Edition, Pearson Education, 2012.<\/em><\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n&nbsp;","rendered":"<div>\n<p><strong>JavaScript &#8211; Event Handlers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">JavaScript&#8217;s interaction with HTML is handled through events that occur when the user or the browser manipulates a page. When the page loads, it is called an event. It allows us to execute an action of code when the event occurs. All the event handlers in JavaScript start with the keyword <strong><em>on<\/em><\/strong>, and each event handler deals with a certain type of event. (For eg, <strong>onClick<\/strong> )<\/p>\n<p>&nbsp;<\/p>\n<p><strong>How it works:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Step 1:\u00a0 \u00a0 User moves mouse over object<\/p>\n<p>Step 2:\u00a0 \u00a0Event senses that something happened to the object<\/p>\n<p>Step 3:\u00a0 \u00a0JavaScript (Event handler) tells the object what to do<\/p>\n<p>Step 4:\u00a0 \u00a0Using DOM, the handler locates the object on the web page<\/p>\n<p>Step 5:\u00a0 \u00a0Object\u2019s image source is changed<\/p>\n<\/div>\n<p style=\"text-align: justify\">Here Step is an action where the user moves the mouse over an object. This action is sensed as an event, and JavaScript handles this event with an event handler. Using DOM, the object is located and the reaction to the event is performed as toggling the source image to anaother one. This is shown in the below diagram.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-156 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-83.png\" alt=\"\" width=\"669\" height=\"85\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-83.png 669w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-83-300x38.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-83-65x8.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-83-225x29.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-83-350x44.png 350w\" sizes=\"auto, (max-width: 669px) 100vw, 669px\" \/><\/p>\n<p style=\"text-align: justify\">The following is the list of event handlers described along with the Objects on which they can be applied and when they are triggered as given in Table 19.1.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>TABLE 19.1: Event Handlers<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-158 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-85.png\" alt=\"\" width=\"510\" height=\"918\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-85.png 510w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-85-167x300.png 167w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-85-65x117.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-85-225x405.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-85-350x630.png 350w\" sizes=\"auto, (max-width: 510px) 100vw, 510px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p>Now let us demonstrate some of the event type with examples and the output of it.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>onClick Event Type<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>This event occurs when a user clicks the left button of his mouse.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 1<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt; script type=&#8221;text\/javascript&#8221;&gt;<\/p>\n<p>function sayHello()<\/p>\n<p>{<\/p>\n<p>alert(&#8220;Hello World&#8221;)<\/p>\n<p>}<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/head&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;body&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;p&gt;Click the following button and see result&lt;\/p&gt; &lt;form&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;input type=&#8221;button&#8221; onclick=&#8221;sayHello()&#8221; value=&#8221;Say Hello&#8221; \/&gt; &lt;\/form&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span><\/p>\n<\/div>\n<p style=\"text-align: justify\">\u00a0 On clicking the button it calls the function sayHello() which alerts a message as shown in FIgure 19.1 and Figure 19.2.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-159 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-86.png\" alt=\"\" width=\"291\" height=\"357\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-86.png 291w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-86-245x300.png 245w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-86-65x80.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-86-225x276.png 225w\" sizes=\"auto, (max-width: 291px) 100vw, 291px\" \/><\/p>\n<p><strong>Example 2<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;title&gt; color world &lt;\/title&gt;<\/p>\n<p>&lt;script language = &#8220;JAVASCRIPT&#8221;&gt;<\/p>\n<p>function color(f, a)<\/p>\n<p>{<\/p>\n<p>document.bgColor=a;<\/p>\n<p>alert(a);<\/p>\n<p>}<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body bgcolor=gray&gt;<\/p>\n<p>&lt;form name=myform&gt;<\/p>\n<p>&lt;input type = button value=red onClick=color(this.form,&#8221;RED&#8221;)&gt;<\/p>\n<p>&lt;input type = button value=orange onClick=color(this.form,&#8221;ORANGE&#8221;)&gt; &lt;input type = button value=green onClick=color(this.form,&#8221;GREEN&#8221;)&gt; &lt;input type = button value=violet onClick=color(this.form,&#8221;VIOLET&#8221;)&gt; &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This example is to demonstrate the onClick event type which changes the background color of the document on clicking the respective buttons as shown in Figure 19.3 and Figure 19.4.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-160 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-87.png\" alt=\"\" width=\"477\" height=\"285\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-87.png 477w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-87-300x179.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-87-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-87-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-87-350x209.png 350w\" sizes=\"auto, (max-width: 477px) 100vw, 477px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-161 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-88.png\" alt=\"\" width=\"506\" height=\"301\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-88.png 506w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-88-300x178.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-88-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-88-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-88-350x208.png 350w\" sizes=\"auto, (max-width: 506px) 100vw, 506px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0onSubmit Event type<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The <strong>onSubmit<\/strong> is an event that occurs when you try to submit a form.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;script type=&#8221;text\/javascript&#8221;&gt;<\/p>\n<p>function validate()<\/p>\n<p>{<\/p>\n<p>all validation goes here<\/p>\n<p>&#8230;&#8230;&#8230;<\/p>\n<p>return either true or false<\/p>\n<p>}<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form method=&#8221;POST&#8221; action=&#8221;t.cgi&#8221; onsubmit=&#8221;return validate()&#8221;&gt; &lt;input type=&#8221;submit&#8221; value=&#8221;Submit&#8221; \/&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>The example demonstates that the form is validated when a submit button is clicked as shown in Figure 19.5.<\/p>\n<\/div>\n<p><strong>\u00a0 \u00a0 Output:<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-164 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-91.png\" alt=\"\" width=\"253\" height=\"127\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-91.png 253w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-91-65x33.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-91-225x113.png 225w\" sizes=\"auto, (max-width: 253px) 100vw, 253px\" \/><\/p>\n<p><strong>onMouseOver and onMouseOut Event<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The <strong>onMouseOver<\/strong> event is triggered when you bring your mouse over any element on the document and the <strong>onMouseOut<\/strong> event is triggered when you move your mouse out from that element.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;script type=&#8221;text\/javascript&#8221;&gt;<\/p>\n<p>function over() {<\/p>\n<p>document.write (&#8220;Mouse Over&#8221;);<\/p>\n<p>}<\/p>\n<p>function out() {<\/p>\n<p>document.write (&#8220;Mouse Out&#8221;);<\/p>\n<p>}<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;p&gt;Bring your mouse inside the division to see the result:&lt;\/p&gt;<\/p>\n<p>&lt;div onmouseover = &#8220;over()&#8221; onmouseout= &#8220;out()\u201c &gt; &lt;h2&gt; This is inside the division &lt;\/h2&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This example demonstrates the onmouseover and onmouseout event types. When the mouse is moved over the text, the function over() is called which displays the message &#8220;Mouse Over&#8221; and when the mouse is moved out of the text , the function out() is called which displays the message &#8220;Mouse Out&#8221;. The output of this example code is shown in the Figure 19.6 and Figure 19.7.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-165 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-92.png\" alt=\"\" width=\"301\" height=\"278\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-92.png 301w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-92-300x277.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-92-65x60.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-92-225x208.png 225w\" sizes=\"auto, (max-width: 301px) 100vw, 301px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 2:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;title&gt; image world &lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body &gt;<\/p>\n<p>&lt;a href=ex.html onMouseOver = document.images[0].src = &#8220;FLOWER.jpg&#8221;<\/p>\n<p>onMouseOut = document.images[0].src = &#8220;GRAPES.jpg&#8221; &gt; &lt;img src=&#8221;GRAPES.jpg&#8221; wi dth=160 height=100 border=1&gt; &lt;\/a&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This example toggles the image on thedocument when the mouse is moved over or moved out of the image displayed. The output of this example code is shown in Figure 19.8 and Figure 19.9.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-166 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-93.png\" alt=\"\" width=\"439\" height=\"512\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-93.png 439w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-93-257x300.png 257w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-93-65x76.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-93-225x262.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-93-350x408.png 350w\" sizes=\"auto, (max-width: 439px) 100vw, 439px\" \/><\/p>\n<p style=\"text-align: center\"><strong>Figure 19.9<\/strong><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p><strong>JavaScript Form Validation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>HTML form validation can be done by writing a script code using JavaScript.<\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 <\/strong><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>An example to validate the name to be filled out without leaving it blank is shown below.<\/p>\n<p>function validateForm() {<\/p>\n<p>var x = document.forms[&#8220;myForm&#8221;][&#8220;fname&#8221;].value; if (x == null || x == &#8220;&#8221;) {<\/p>\n<p>alert(&#8220;Name must be filled out&#8221;);<\/p>\n<p>return false;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>JavaScript Forms<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;form name=&#8221;myForm&#8221; action=&#8221;demo_form.asp&#8221; <strong>onsubmit=&#8221;return<\/strong> <strong>validateForm()&#8221; <\/strong>method=&#8221;post&#8221;&gt;<\/p>\n<p>Name: &lt;input type=&#8221;text&#8221; name=&#8221;fname&#8221;&gt;<\/p>\n<p>&lt;input type=&#8221;submit&#8221; value=&#8221;Submit&#8221;&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;script type=&#8221;text\/javascript&#8221;&gt;<\/p>\n<p>function newValue()<\/p>\n<p>{<\/p>\n<p>var x=document.forms.myForm<\/p>\n<p>x[0].value=&#8221;The mere act of aiming at something makes you big&#8221;<\/p>\n<p>x[0].select()<\/p>\n<p>}<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form name=&#8221;myForm&#8221;&gt;<\/p>\n<p>&lt;textarea name=&#8221;myTextarea&#8221; rows=&#8221;10&#8243; cols=&#8221;20&#8243;&gt; The roots of e ducation are bitter but the fruit is sweeter &lt;\/textarea&gt;&lt;br \/&gt;<\/p>\n<p>&lt;input type=&#8221;button&#8221; onclick=&#8221;newValue()&#8221; value=&#8221;New Value&#8221;&gt; &lt;\/form&gt;&lt;\/body&gt;&lt;\/html&gt;<\/p>\n<\/div>\n<p style=\"text-align: justify\">This example is to demonstrate about validating a form which has a textarea. The textarea is initially shown with the text. On clicking the button, the function newValue() is called which displays another text. The method select() is used to display the textarea as selected. The output is shown in FIgure 19.10.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-167 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-94.png\" alt=\"\" width=\"608\" height=\"393\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-94.png 608w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-94-300x194.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-94-65x42.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-94-225x145.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-94-350x226.png 350w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><\/p>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0Example 2:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;title&gt; Forms&lt;\/title&gt;<\/p>\n<p>&lt;!&#8211;This code checks the Checkbox When the button is clicked &#8211;&gt;<\/p>\n<p>&lt;script Language=&#8217;JavaScript&#8217;&gt;<\/p>\n<p>function Chk(f1)<\/p>\n<p>{<\/p>\n<p>f1.Check.checked=true;<\/p>\n<p>window.alert(&#8220;The Checkbox just got checked&#8221;);<\/p>\n<p>f1.Check.checked=false;<\/p>\n<p>f1.Radio[0].checked=true;<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">f1.Radio[1].checked=false;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">window.alert(&#8220;The Radio button just got checked&#8221;);<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/script&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/head&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;body&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;form&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Client Name :\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;Input Type =Text Name=&#8221;Text&#8221; Value=&#8221;&#8221;&gt;&lt;br&gt;&lt;br&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">Client Address : &lt;Input Type =Text Name=&#8221;Text1&#8243; Value=&#8221;&#8221;&gt;&lt;br&gt;&lt;br&gt; Client E-mail Address : &lt;Input Type =Text Name=&#8221;Text2&#8243; Value=&#8221;&#8221;&gt;&lt;br&gt;&lt;br&gt; &lt;Input Type=&#8221;radio&#8221; Name=&#8221;Radio&#8221; Value=&#8221;&#8221;&gt; Male<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;Input Type=&#8221;radio&#8221; Name=&#8221;Radio&#8221; Value=&#8221;&#8221;&gt; Female&lt;br&gt;&lt;br&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;Input Type=\u201ccheckbox&#8221; Name=&#8221;Check&#8221; Value=&#8221;&#8221;&gt; Em ploye d &lt;br&gt;&lt;br&gt; &lt;Input Type=&#8221;Button&#8221; Name=&#8221;Bt&#8221; Value=&#8221;Set Element Array Value&#8221;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">onClick=&#8221;Chk(this.form)&#8221;&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/form&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span><\/p>\n<\/div>\n<p style=\"text-align: justify\">This example is to demonstrate about validating a form which has a checkbox or a radiobutton. If checkbox is checked or radiobutton is checked, the property &#8220;checked&#8221; is made true and it will display the message as checked. The output is shown in Figure 19.11.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-168 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-95.png\" alt=\"\" width=\"584\" height=\"348\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-95.png 584w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-95-300x179.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-95-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-95-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-95-350x209.png 350w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example 3:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;script language=&#8221;javascript&#8221;&gt;<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form&gt;<\/p>\n<p>FirstName:&lt;input type=&#8221;text&#8221; name=&#8221;Firstname&#8221; size=20 onFocus=&#8221;this.blur();&#8221;&gt;<\/p>\n<p>LastName:&lt;input type=&#8221;text&#8221; name=&#8221;Lastname&#8221; size=20&gt;&lt;p&gt;<\/p>\n<p>Address:&lt;input type=&#8221;text&#8221; name=&#8221;Address&#8221; size=60&gt;&lt;p&gt; pincode:&lt;input type=&#8221;text&#8221; name=&#8221;Pincode&#8221; size=6&gt;&lt;p&gt; &lt;input type=&#8221;button&#8221; name=&#8221;act&#8221; value=&#8221;verify&#8221;&gt; &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This example code demonstrates the use of function onFocus on a textfield. When a focus is gained on the textfield, it is made to blur using the function blur() which will not allow to enter any data in the textfield. The output is shown in Figure 19.12.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-169 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-96.png\" alt=\"\" width=\"587\" height=\"347\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-96.png 587w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-96-300x177.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-96-65x38.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-96-225x133.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-96-350x207.png 350w\" sizes=\"auto, (max-width: 587px) 100vw, 587px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p><strong>Example 4:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;title&gt;forms1&lt;\/title&gt;<\/p>\n<p>&lt;script&gt;<\/p>\n<p>function func(f1)<\/p>\n<p>{<\/p>\n<p>alert(&#8220;the form elements have been cleared&#8221;);<\/p>\n<p>}<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form onReset=&#8221;func(this.form)&#8221;&gt;<\/p>\n<p>Client Name: &lt;input type=&#8221;text&#8221; name=&#8221;text&#8221; value=&#8221;&#8221;&gt;&lt;br&gt;&lt;br&gt;<\/p>\n<p>Client address: &lt;input type=&#8221;text&#8221; name=&#8221;text1&#8243; value=&#8221;&#8221;&gt;&lt;br&gt;&lt;br&gt; &lt;input type=&#8221;radio&#8221; name=&#8221;radio&#8221; value=&#8221;&#8221;&gt;male<\/p>\n<p>&lt;input type=&#8221;radio&#8221; name=&#8221;radio&#8221; value=&#8221;&#8221;&gt;female&lt;br&gt;&lt;br&gt;<\/p>\n<p>&lt;input type=&#8221;checkbox&#8221; name=&#8221;check&#8221; value=&#8221;&#8221;&gt;employed&lt;br&gt;&lt;br&gt; &lt;input type=&#8221;reset&#8221; name=&#8221;rst&#8221; value=&#8221;reset&#8221;&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This example code demonstrates validating a form on a reset button. The event onReset is triggered when a reset button is clicked which clears the form content and displays the message as cleared. The output is shown in Figure 19.13<\/p>\n<\/div>\n<p><strong>\u00a0 \u00a0 Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-170 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-97.png\" alt=\"\" width=\"574\" height=\"325\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-97.png 574w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-97-300x170.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-97-65x37.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-97-225x127.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-97-350x198.png 350w\" sizes=\"auto, (max-width: 574px) 100vw, 574px\" \/><\/p>\n<div>\n<p><strong>Example 5:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;title&gt; using text and button objects&lt;\/title&gt;<\/p>\n<p>&lt;script language=&#8221;javascript&#8221;&gt;<\/p>\n<p>function calculate(form)<\/p>\n<p>{<\/p>\n<p>form.results.value= eval(form.entry.value * 10 );<\/p>\n<p>}<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form&gt;<\/p>\n<p>&lt;input type=&#8221;text&#8221; name=&#8221;entry&#8221; value=&#8221;&#8221;&gt;<\/p>\n<p>&lt;input type=&#8221;button&#8221; value=&#8221;calculate&#8221; onClick=&#8221;calculate(this.form);&#8221;<\/p>\n<p>&lt;br&gt;<\/p>\n<p>&lt;input type=&#8221;text&#8221; name=&#8221;results&#8221; onFocus=&#8221;this.blur();&#8221;&gt; &lt;br&gt;<\/p>\n<p>&lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<\/div>\n<p style=\"text-align: justify\">This example code demonstrates the use of onClick() and onFocus(). The onFocus is applied on results textfield which will not allow to enter any value using the blur() method. The value entered on the entry field is calculated and shown in the result field. The output of this code is shown in Figure 19.14.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-171 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-98.png\" alt=\"\" width=\"603\" height=\"376\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-98.png 603w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-98-300x187.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-98-65x41.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-98-225x140.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-98-350x218.png 350w\" sizes=\"auto, (max-width: 603px) 100vw, 603px\" \/><\/p>\n<p>&nbsp;<\/p>\n<div>\n<p><strong>Example 6:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;htMl&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;title&gt;working with checkboxes&lt;\/title&gt;<\/p>\n<p>&lt;script&gt;<\/p>\n<p>function calculate(form, callingField)<\/p>\n<p>{<\/p>\n<p>if(callingField==&#8221;result&#8221;)<\/p>\n<p>{<\/p>\n<p>if(form.square.checked)<\/p>\n<p>{<\/p>\n<p>form.entry.value= Math.sqrt(form.result.value);<\/p>\n<p>}<\/p>\n<p>else<\/p>\n<p>{<\/p>\n<p>form.entry.value=form.result.value\/2;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p><span style=\"font-size: 1em;text-align: initial\">else {<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">if(form.square.checked)<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">{<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">form.result.value = form.entry.value * form.entry.value;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">E<\/span><span style=\"text-align: initial;font-size: 1em\">lse {<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">form.result.value = form.entry.value*2;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">}}<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/script&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/head&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;body&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;form&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;center&gt;&lt;br&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;b&gt; value: &lt;\/b&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;input type =&#8221;text&#8221; name = &#8220;entry&#8221; value = 0 onChange =&#8221;calculate(this.form,this.name);&#8221;&gt; &lt;br&gt;&lt;br&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;b&gt;action&lt;\/b&gt;(default-double):<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;input type =&#8221;checkbox&#8221; name = &#8220;square&#8221; onClick = &#8220;calculate(this.form,this.name);&#8221;&gt;square &lt;br&gt;&lt;br&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;b&gt; result: &lt;\/b&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;input type =&#8221;text&#8221; name = &#8220;result&#8221; value = 0 onChange = &#8220;calculate(this.form,this.name);&#8221;&gt; &lt;\/center&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/form&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/body&gt;<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">&lt;\/html&gt;<\/span><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">This example code demonstrates the use of onClick and onChange event types. There are two textfields (entry and result) and a radiobutton in the form. If the calling field is result field, and if the checkbox is checked then it calculates the squareroot of the value entered in the result field and displays in the entry field else it calculates half of the result value and displays in the entry field. Similarly when the value entered in the entry field changes, based on the checkbox checked or not checked displays the square of the value entered in the entry field or multiplies the value by 2 and displays in the result field. The output of this code is shown in Figure 19.15.<\/p>\n<\/div>\n<p><strong>\u00a0 \u00a0 Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-172 aligncenter\" src=\"http:\/\/csp12.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-99.png\" alt=\"\" width=\"609\" height=\"384\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-99.png 609w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-99-300x189.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-99-65x41.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-99-225x142.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-content\/uploads\/sites\/60\/2018\/07\/Untitled-99-350x221.png 350w\" sizes=\"auto, (max-width: 609px) 100vw, 609px\" \/><\/p>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0JavaScript Cookies<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A cookie is used to store information on the user\u2019s computer even when the user switches off his\/her computer. It is a data that is sent from a web server to a web browser when the user visits a site on a server. It is just a .txt file stored in a user\u2019s computer. A cookie can be associated with one or more documents on the web server. More than one cookie can be associated with a document on the web server. Every cookie has a NAME-VALUE pair associated with it. Cookies have an expiration date associated with them.<\/p>\n<p>&nbsp;<\/p>\n<p>Cookies are a plain text data record of 5 variable-length fields:<\/p>\n<p>&nbsp;<\/p>\n<p>1.<strong>Expires <\/strong>\u2212 The date the cookie will expire. If this is blank, the cookie will expire when the visitor quits the browser.<\/p>\n<p>2.<strong>Domain <\/strong>-The domain name of your site.<\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">3.\u00a0<\/span><strong style=\"text-align: initial;font-size: 1em\">Path <\/strong><span style=\"text-align: initial;font-size: 1em\">\u2212 The path to the directory or web page that set the cookie. This may be blank if you want to retrieve the cookie from any directory or page.<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">4.<\/span><strong style=\"text-align: initial;font-size: 1em\">Secure <\/strong><span style=\"text-align: initial;font-size: 1em\">\u2212 If this field contains the word &#8220;secure&#8221;, then the cookie may only be retrieved with a secure server. If this field is blank, no such restriction exists.<\/span><\/p>\n<p><span style=\"text-align: initial;font-size: 1em\">5.\u00a0<\/span><strong style=\"text-align: initial;font-size: 1em\">Name-Value <\/strong><span style=\"text-align: initial;font-size: 1em\">&#8211; Cookies are set and retrieved in the form of key-value pairs.<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 \u00a0Create Cookies<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>We can create a cookie by assigning a string value to the document.cookie object.document.cookie = &#8220;key1=value1;key2=value2;expires=date&#8221;;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Store Cookies<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Example<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;script type=&#8221;text\/javascript&#8221;&gt;<\/p>\n<p>function WriteCookie()<\/p>\n<p>{<\/p>\n<p>if( document.myform.customer.value == &#8220;&#8221; )<\/p>\n<p>{<\/p>\n<p>alert(&#8220;Enter some value!&#8221;);<\/p>\n<p>return;<\/p>\n<p>}<\/p>\n<p>cookievalue= escape(document.myform.customer.value) + &#8220;;&#8221;; document.cookie=&#8221;name=&#8221; + cookievalue;<\/p>\n<p>document.write (&#8220;Setting Cookies : &#8221; + &#8220;name=&#8221; + cookievalue );<\/p>\n<p>}<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;form name=&#8221;myform&#8221; action=&#8221;&#8221;&gt;<\/p>\n<p>Enter name: &lt;input type=&#8221;text&#8221; name=&#8221;customer&#8221;\/&gt;<\/p>\n<p>&lt;input type=&#8221;button&#8221; value=&#8221;Set Cookie&#8221; onclick=&#8221;WriteCookie();&#8221;\/&gt; &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The example shows how a cookie can be created and stored. It retrieves the value from the textfield and stores it as a cookie.<\/p>\n<\/div>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>&lt;script type=&#8221;text\/javascript&#8221;&gt;<\/p>\n<p>function ReadCookie()<\/p>\n<p>{<\/p>\n<p>var allcookies = document.cookie;<\/p>\n<p>document.write (&#8220;All Cookies : &#8221; + allcookies );<\/p>\n<ul>\n<li>\/\/ Get all the cookies pairs in an array cookiearray = allcookies.split(&#8216;;&#8217;);<\/li>\n<li>\/\/ Now take key value pair out of this array for(var i=0; i&lt;cookiearray.length; i++)<\/li>\n<\/ul>\n<p>{<\/p>\n<p>name = cookiearray[i].split(&#8216;=&#8217;)[0]; value = cookiearray[i].split(&#8216;=&#8217;)[1];<\/p>\n<p>document.write (&#8220;Key is : &#8221; + name + &#8221; and Value is : &#8221; + value);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The function ReadCookie() retrieve the stored cookie from document.cookie and store in a variable.Then we can use a for loop to read each cookie as a key-value pair.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">This module explains about the JavaScript Event handling mechanisms. This module also explores about working with HTML form validation using HTML DOM and Event handling and finally discusses about JavaScript Cookies.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Web Links<\/strong><\/p>\n<ul>\n<li><em>http:\/\/www.w3schools.com\/<\/em><\/li>\n<li><em>www.doc.ic.ac.uk\/~rcheung\/teaching\/2720\/ppt\/lecture04c.ppt<\/em><\/li>\n<li><em>Paul J. Deitel, Harvey M. Deitel, Abbey Deitel, &#8220;Internet and World Wide Web How To Program&#8221;, 5th Edition, Pearson Education, 2012.<\/em><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"author":4,"menu_order":17,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-m-vijayalakshmi"],"pb_section_license":""},"chapter-type":[],"contributor":[58],"license":[],"class_list":["post-155","chapter","type-chapter","status-publish","hentry","contributor-dr-m-vijayalakshmi"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/users\/4"}],"version-history":[{"count":2,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/155\/revisions"}],"predecessor-version":[{"id":546,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/155\/revisions\/546"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapters\/155\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/media?parent=155"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/pressbooks\/v2\/chapter-type?post=155"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/contributor?post=155"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp12\/wp-json\/wp\/v2\/license?post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}