Question
Forms and Form Elements Part II The Radio Object When creating radio buttons, each group of buttons must have the same name. The radio buttons
Forms and Form Elements Part II
The Radio Object
- When creating radio buttons, each group of buttons must have the same name.
- The radio buttons basically become assigned to an array (numbering starting with 0 for the first button and counting up from there.
- To determine how many radio buttons are in a group you would use the length property:
document.radioForm.groupName.length
- To determine if a specific box has been highlighted, use the checked property:
document.radioForm.groupName[0].checked
- In the following script each of the 3 stooges has their full name assigned to a value. When the user clicks on the button, a function is called which runs an if loop which checks the checked property of each button. Once it finds the checked property, the loop breaks and the value of i is left at the current value of the radio button array. The alert box then looks up the i th value property so that the full name can be displayed in the alert box.
function fullName() {
for (var i=0; i if (radioForm.stooges[i].checked) { break } } alert("You chose " + radioForm.stooges[i].value + ".") }
The Select Object
- In a selection menu, some properties belong to the entire SELECT box, others belong to individual items in the list.
- To determine which item in the menu has been selected, you can use the value property of the SELECT object as follows:
document.selectForm.selectName.value
- In the following script, the values of each item in the list are URLs which will take a user to those locations, invoked by the onChange event handler.
function goThere() {
window.location=document.selectForm.urlList.value
}
Prevalidating Form Information
- This script uses the forms onSubmit event handler which fires when one clicks on the Submit button.
- The function checks to make sure all fields are filled in. If any are not, the function reads return false which means the form will not be sent.
function checkForm() {
for (var i = 0; i < validForm.elements.length-1; i++) {
if (validForm.elements[i].value == "") {
alert("Fill out ALL fields.")
return false
}
}
return true
}
Assignment 8
Create a page that includes a SELECT object to change the background color of the current page. The property that you need to set is document.bgColor, and the three values you should offer are red, yellow, and green. The menu items should read Stop, Caution, and Go. If you choose menu item Stop then the page should turn red, etc. Make sure to change all your variable names, form name, etc. to match what you are doing in your assignment.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started