tags:

views:

77

answers:

3

Hi all,

I am trying to retreive the value from select tag for selected option in Javascript

This is working fine in Mozilla but not in IE7. Example:

function uploadFiles()
{
    var x = document.getElementById('fileName').value;
    alert(x);
}
+3  A: 

If it's a <select> then you should be using:

var s = document.getElementById('fileName');
var x = s.options[s.selectedIndex].value;
Greg
A: 

You need to specify a value attribute on each <option>: http://jsbin.com/uluki (http://jsbin.com/uluki/edit to edit):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Sandbox</title>
</head>
<body>
  <select id="fileName">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>

  <a href="#" onclick="alert(document.getElementById('fileName').value); return false">Show selected value</a>
</body>
</html>

All I do is alert(document.getElementById('fileName').value). Works in IE6, IE7, IE8, Opera 9, FF2, FF3+, Chrome 2+ (and probably every other browser). It's defacto now, again as long as you have a value on each <option>.

If you don't have a value on each option, Greg's answer is the way to go. Works everytime.

Roatin Marth
A: 

thank all,

     This is working fine for the single value. How can i get multiple value using javascript. If i have tag multiple = "multiple"
Hemant