...

View Full Version : Enable field via Drop Down Selection



jforena
01-18-2008, 02:10 AM
Does anyone have an example of a script which will enable a "state" text field when "United States" is selected from a "country" drop down selection? By default "state" would be disabled.

Thanks for any help!

Kor
01-18-2008, 10:29 AM
Like this?:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function controlField(obj){
var t=obj.form['state'];
if(obj.value.match(/United States/)){
t.removeAttribute('disabled');
t.focus();
}
else{
t.value='';
t.setAttribute('disabled','disabled');
}
}
</script>
</head>
<body>
<form>
<select onchange="controlField(this)">
<option value="">- country -</option>
<option value="Canada">Canada</option>
<option value="United States">United States</option>
<option value="UK">UK</option>
</select>U.S State<input type="text" disabled="disabled" name="state">
</form>
</body>
</html>

jforena
01-18-2008, 09:51 PM
Thanks for the info. This is very close. Is it possible to add more than one country? For example, if the United States or Canada was selected it would enable the State text field.

Thanks again!

Kor
01-18-2008, 10:45 PM
...
if(obj.value.match(/United States|Canada/)){
...

See also RegExp:
http://lawrence.ecorp.net/inet/samples/regexp-intro.php

jforena
01-18-2008, 11:32 PM
Way cool, thank you Kor!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum