710 likes | 909 Views
More JavaScript. JavaScript and HTML forms. One of the most common usages of JavaScript is to respond to user actions when completing HTML forms JavaScript enables us to process HTML forms by providing a large library of object classes with associated methods and attributes
E N D
JavaScript and HTML forms • One of the most common usages of JavaScript is to respond to user actions when completing HTML forms • JavaScript enables us to process HTML forms by providing • a large library of object classes • with associated methods and attributes • This library is part of a much bigger library, the DOM (Document Object Model) library that will be introduced in CS 4408 • Now, however, we will consider just a small part of it, connected with HTML form elements
Processing input elements • We have seen that we can access any HTML element on a page if it has an id attribute • Remember the form which contained this: <p> What is your name? <input type=‘text’ name=‘name’ id=‘name’ > </p> • We could check if its value is the empty string with this JavaScript statement if ( document.getElementById(‘name’).value =='') { alert("Name is empty"); }
The JavaScript object class for representinginput elements • We have already seen that the this object class contains an attribute called value • We have just read the value of this attribute • But we can also change its value • Consider the form on the next slide • which asks for the colour of the user’s hair and eyes
The user gives his hair colour as ‘brown’ and hits the tab key on his keyboard
The cursor flashes in the eye-colour input box, which is still empty, waiting for him to type something
Suppose the user declares his hair colour is ‘red’.Before he hits the tab key, the cursor is still in the hair-colour box and the eye-colour box is empty
But, immediately after he hits the tab key, the value ‘blue’ appears in the eye-colour box
Although, if he wants, the user can replace the eye-colour ‘blue’ with some other colour
Specification for the form we have just seen <html> <head> <style>form { background-color:yellow; width:300; padding:0.1in }</style> <script> function respond() { var hairColour=document.getElementById('hairColour'); var eyeColour=document.getElementById('eyeColour'); if (hairColour.value=='red') {eyeColour.value='blue';} } </script> </head> <body> <form method="post" action="someProgramURL"> <fieldset><legend>Colour of your hair?</legend> <input type="text" name="hairColour" id="hairColour“ onchange="respond()" > </fieldset> <fieldset><legend>Colour of your eyes?</legend> <input type="text" name="eyeColour" id="eyeColour"> </fieldset> <button type="Submit">Submit details</button> </form> </body> </html>
Attributes and methods of the HTMLInputElement class Attributes DOMString defaultValue; boolean defaultChecked; readonly HTMLFormElement form; DOMString accept; DOMString accessKey; DOMString align; DOMString alt; boolean checked; boolean disabled; long maxLength; DOMString name; boolean readOnly; DOMString size; DOMString src; long tabIndex; readonly DOMString type; DOMString useMap; DOMString value; Methods void blur() ; void focus() ; void select() ; void click() ;
Notice that some attributes are readonly Attributes DOMString defaultValue; boolean defaultChecked; readonly HTMLFormElement form; DOMString accept; DOMString accessKey; DOMString align; /*deprecated from HTML 4.0 onwards*/ DOMString alt; boolean checked; boolean disabled; long maxLength; DOMString name; boolean readOnly; DOMString size; DOMString src; long tabIndex; readonly DOMString type; DOMString useMap; DOMString value; • The form attribute of a HTMLInputElement object lets us access the object which represents the entire form of which the input element forms a part • The type attribute of a HTMLInputElement object gives us the type of the input element • We cannot change either of these
Using the size attribute • Remember that Spanish names are often very long • We might want to cater for that on a form which asks where the user comes from and what his name is
Suppose the user has stated he is from ‘Ireland’ but has not yet hit the tab key
After he has hit the tab key, the size of the input element for entering his name is unchanged
After he has hit the tab key, the size of the input element for entering his name is unchanged
Suppose the user declares she is from Spain, but has not yet hit the tab key
After she has hit the tab key, the box in which she can enter her name gets much bigger
Specification of previous form <html> <head> <style> form { background-color:yellow; width:600; padding:0.1in } </style> <script> function respond() {var country=document.getElementById('country'); var name=document.getElementById('name'); if (country.value=='Spain') { name.size=80; } } </script> </head> <body> <form method="post" action="someProgramURL"> <fieldset><legend>What country are you from?</legend> <input type="text" name="country" id="country“ onchange="respond()" > </fieldset> <fieldset><legend>What is your name?</legend> <input type="text" name="name" id="name"> </fieldset> <button type="Submit">Submit details</button> </form> </body> </html>
Sometimes, people from other countries have long nameWe could let the user declare she has a long name, by clicking on a checkbox
When the user has declared her country and hit the tab key, focus moves to the checkbox
The user has clicked the checkbox but has not yet hit the tab key
The user has just hit the tab key and the name box has become bigger
Specification of previous form <html> <head> <style> form { background-color:yellow; width:600; padding:0.1in } </style> <script> function respond() {var longName=document.getElementById('longName'); var name=document.getElementById('name'); if (longName.checked) { name.size=80; } } </script> </head> <body> <form method="post" action="someProgramURL"> <fieldset><legend>What country are you from?</legend> <input type="text" name="country" id="country"> </fieldset> <fieldset><legend>Your name?</legend> Long name? <input type="checkbox" name="longName" id="longName“ onchange="respond()" > <br> <input type="text" name="name" id="name"> </fieldset> <button type="Submit">Submit details</button> </form> </body> </html>
On this form, the company is trying to push sales of Superman’s Cloakso it is pre-checked on the form
Suppose the user wants only Batman’s Cloak,so he clicks on the first checkbox, and then …
and thenwhen he clicks on the box for entering his name, an alert box pops us
Specification of previous form <html> <head> <style>form { background-color:yellow; width:600; padding:0.1in } li { list-style-type : none } </style> <script> function respond(someString) { var clickedItem=document.getElementById(someString); if (clickedItem.defaultChecked && !clickedItem.checked) { alert("Are you really sure you do not want Superman's cloak?"); } } </script> </head> <body> <form method="post" action="someProgramURL"> <fieldset><legend>Your T-shirt selection?</legend> <ul> <li>Batman's cloak <input type="checkbox" name="products" id="products1" value='batman' onchange="respond('products1')" > </li> <li>Superman's cloak <input type="checkbox" name="products" id="products2" value='superman' onchange="respond('products2')“checked='checked‘ >(our most popular item) </li> <li>Dr. Who's coat <input type="checkbox" name="products" id="products3" value='drWho' onchange="respond('products3')" > </li> </ul> </fieldset> <fieldset><legend>What is your name?</legend> <input type="text" name=“name" id=“name"> </fieldset> <button type="Submit">Submit order</button> </form> </body> </html>
We can generalize the previous approach <html> <head> <style>form { background-color:yellow; width:600; padding:0.1in } li { list-style-type : none } </style> <script> function respond(someString) {var clickedItem=document.getElementById(someString); if (clickedItem.defaultChecked && !clickedItem.checked) { alert("Are you really sure you do not want "+clickedItem.value+"?"); } } </script> </head> <body> <form method="post" action="someProgramURL"> <fieldset> <legend>Your T-shirt selection?</legend> <ul> <li> Batman's cloak <input type="checkbox" name="products" id="products1" value="Batman's cloak" onchange="respond('products1')" > </li> <li> Superman's cloak <input type="checkbox" name="products" id="products2" value="Superman's cloak" onchange="respond('products2')" checked='checked' >(our most popular item)</li> <li> Dr. Who's coat <input type="checkbox" name="products" id="products3" value="Dr. Who's coat" onchange="respond('products3')“ checked='checked‘ >(another very popular item)</li> </ul> </fieldset> <fieldset><legend>What is your name?</legend><input type="text" name="country" id="country"></fieldset> <button type="Submit">Submit order</button> </form> </body> </html>
It would be better if we did not have a special button for checking that the form is completed correctly • It would be better if the form-checking task could be performed by the button that we use to submit the data on the form • This button should • Check whether the form is completed correctly • If it is, the data should be submitted • If it is not, a warning message should be produced
User specifies country and clicks on button …Now get a different warning
User specifies both country and nameand clicks on the button and …