Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to copy file 14 54
How to build a web site 17 60
what should i learn? javascript or typescript? 3 41
REST call Failing 1 16
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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

730 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