Solved

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

Posted on 2012-03-29
4
211 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

760 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

24 Experts available now in Live!

Get 1:1 Help Now