Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

Need to alert the value of a <select> onchange, how do I do it?

Hello,

When the user selects a car in the second drop down menu, I would like the value of the two select boxes to alert.  How do I do that?  below is the code?

<!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>
<script type="text/javascript">
function printNames(a) {
alert(a)

};
</script>

</head>

<body>
<select name="car1" id="car1">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<select name="car2"  onchange="printNames(this.options[this.selectedIndex].value, document.getElementById('car1').options[this.selectedIndex].value">
  <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
NewWebDesigner
Asked:
NewWebDesigner
  • 2
1 Solution
 
Ronak PatelCommented:
try this:

<!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>
<script type="text/javascript">
function printNames(objSelect2, objSelect1) 
{
	var car1 = objSelect1.value;
	var car2 = objSelect2.value;

	alert("Car 1: " + car1 + "\n\nCar 2: " + car2);

}
</script>

</head>

<body>
<select name="car1" id="car1">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

<select name="car2" id="car2" onchange="printNames(this, document.getElementById('car1'));">
  <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
 
MrunalCommented:
Here is your solution:

http://jsfiddle.net/ECzsg/
0
 
MrunalCommented:
For javascript version:

http://jsfiddle.net/ECzsg/1/
0
 
nap0leonCommented:
I'd amend mroonal's code with an option for you in case you want the text that the user selected rather than the value of the drop-down. (e.g., you want "Audi" instead of "audi"), use the second alert in the printNames function.

<script type="text/javascript">
function printNames()  
{       
    var car1 = document.getElementById('car1');
    var car2 =document.getElementById('car2');
    alert("Car 1: " + car1.value+ "\n\nCar 2: " + car2.value);
    alert("Car 1: " + car1.options[car1.selectedIndex].text+ "\n\nCar 2: " + car2.options[car2.selectedIndex].text);
}
    </script>
</head>
<body>
<select name="car1n" id="car1" onchange='printNames();'>   
    <option value="volvo">Volvo</option>   
    <option value="saab">Saab</option>   
    <option value="mercedes">Mercedes</option>   
    <option value="audi">Audi</option>
</select>  
<select name="car2n" id="car2" onchange="printNames();">   
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>   
    <option value="mercedes">Mercedes</option>   
    <option value="audi">Audi</option>
</select>
</body>

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now