Solved

Need to change the value of a variable based on a selection box.  (easy)

Posted on 2012-03-29
4
212 Views
Last Modified: 2012-06-28
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?

<!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>

Open in new window

0
Comment
Question by:NewWebDesigner
4 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37785551
yes, it does. Declaring a variable outside function (or globally) make sure that other methods can also update this variable value
0
 
LVL 35

Expert Comment

by:YZlat
ID: 37786857
looks fine. Are you getting any errors? I am not sure what is your question here
0
 
LVL 20

Accepted Solution

by:
thehagman earned 250 total points
ID: 37786890
BTW, how yould you have tested this yourself: Add sime other input item that simply calls "alert(url)". You'd have noted that this alrt would always show tha last change made by the select (or "" initially)
0
 
LVL 53

Assisted Solution

by:COBOLdinosaur
COBOLdinosaur earned 250 total points
ID: 37788971
Your onchange is incorrect the select does not have a value attribute the corrrect syntax is:
onchange="changeUrl(this.options[this.selectedIndex].value;"


Cd&
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
count download link and run update query 9 57
jQuery detect if it is a mobile device 3 104
JS Plugin 4 29
JavaScript export function not working in IE 15 37
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now