Chapter 7
If using the DOM Level 0 events, returning
false
from the event handler and the event-handler script cancels the submittal. If using DOM Level 2, setcancelBubble
totrue
for IE, and call thepreventDefault
for other browsers, both based on the event object.The blur event is triggered when the field loses focus. This is a good time to check the text field to make sure it has valid data.
The select options are stored in an array called
Options
. As such, you can add new options as you would add new array elements, making sure that the entry is a newOption
object:opts[opts.length] = new Option("Option Four", "Opt 4");
HereâÂÂs one approach:
var rgEx = /^[A-Za-z\s]*$/g; var OK = rgEx.exec(document.someForm.text1.value);
The code must first assign an event-handler function to each radio buttonâÂÂs
onclick
event handler:document.someForm.radiogroup[0].onclick=handleClick; document.someForm.radiogroup[1].onclick=handleClick;
If there are several buttons, this can be managed in a
for
loop. In thehandleClick
function, test the check status, and disable the form element accordingly. For instance, to disable the submit button:function handleClick(âÂÂâÂÂ) { if (document.someForm.radiogroup[1].checked) { document.someForm.submit.disabled=true; } else { document.someForm.submit.disabled=false; } }
Get Learning JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.