JavaScript: Working with the <select> tag

  • Thread starter Thread starter Blake
  • 1 comments
  • 690 views

Blake

Premium
Messages
10,976
Australia
NSW, Australia
Messages
haswell00
I&#8217;m having trouble how to figure out how to work with the <select> and <option> tags.

I am using the following function for various errors in a form.

Code:
function error(f, message) {
	alert(message);
	f.select();
}

Anyway, part of the form has some drop down boxes, and if certain conditions are met I use my error function.

My problem is that I need to pass something to f in my error function. I&#8217;m not sure what to pass to it from a <select> tag&#8230;

The part in bold and red is what I&#8217;m having trouble with. I hope I made myself clear. thanks. 👍

Code:
if(f.delDays.options.selectedIndex == today) {
	error([COLOR="DarkRed"][B]f.delDays[/B][/COLOR], 'We require 1 day notice on deliveries. Please select another day.');
	return false;
}
else {
	if(f.delTime.options.selectedIndex < 4 || f.delTime.options.selectedIndex > 24) {
		error([COLOR="DarkRed"][B]f.delTime[/B][/COLOR], 'We can\u2019t deliver before 10am or after 3pm.');
		return false;
	}
}
 
Use document.getElementById(<fieldname>)

Note the case, particularly the lowercase 'd'. The <fieldname> can be passed either as a string or a variable.

You can access the attributes of the item using the normal way, so document.getElementById('drivera').value would be equal to the value of whatever form field ID 'drivera' was at the time that the function was called.

You can also set values, using document.getElementById(<fieldname>).setAttribute(<attributename>, <value>).

Googling "getElementById" should give you enough information to move on from these crude examples.
 
Back