NewWebDesigner
asked on
Need to change the value of a variable based on a selection box. (easy)
Hello,
I want to set the value of a variable outside of the scope of a function. Does this code accomplish that? What is a simple way that I can tell?
I want to set the value of a variable outside of the scope of a function. Does this code accomplish that? What is a simple way that I can tell?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script type="text/javascript">
var url="";
function changeUrl(a) {
if (a.value== "volvo") {
url = "volvoPage.php"
alert(url);
return;
}
else { url= "saab.php"
alert(url)
return;}
}
</script>
<body>
<select onchange="changeUrl(this)">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
</body>
</html>
yes, it does. Declaring a variable outside function (or globally) make sure that other methods can also update this variable value
looks fine. Are you getting any errors? I am not sure what is your question here
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.