views:

616

answers:

5

Hi All,

unable to get selected value from list box in IE 8

var myvalue= document.getElementById("fileName").value;
alert(myvalue);
+1  A: 
var listbox = document.getElementById("list");
for(var i=0; i<listbox.options.length; i++)
    if(listbox.options[i].selected)
        alert(listbox.options[i].value);

Something like that?

Edit: typo!

Robert Grant
yes something like this but it not working in my case.......
ank
+3  A: 
var select = document.getElementById("fileName");
var myvalue= select.options[select.selectedIndex].value;
alert(myvalue);

select.value appears in the DOM specification, but has never been implemented by IE. The selectedIndex property works everywhere.

UPDATE: as anddoutoi points out in a comment to the original question, this assumes you are using a single-item select.

NickFitz
Not returning any value
ank
A: 
var selectElement = document.getElementById("selectElementId");
var selectedValue = selectElement.options[selectElement.selectedIndex].value;
BalusC
Not returning any value
ank
+2  A: 

Your problem is with your HTML. Quoted from your comment:

No simple Option box <select id="fileName" style="width: 100%;" size="3" name="uploadedfile"> <option id="my1Div">test1</option> <option id="my3Div">test2</option> <option id="my5Div">test3</option> </select>

You have to actually specify a value for each <option>. Try this:

<select id="fileName">
    <option id="my1Div" value="test1">test1</option>
    ...
    <option id="my5Div" value="test3">test3</option>
</select>
MalphasWats
hi thanks i was creating option in runtime now i have also add the value attribute it is working..........
ank
Actually the `value` attribute is optional: it defaults to the content of the option. http://www.w3.org/TR/REC-html40/interact/forms.html#adef-value-OPTION
NickFitz
**should** I just tested it, works in Firefox but IE8 doesn't default to the content.
MalphasWats
I should add: When you're getting the value from `document.getElementById('fileName').value`. I didn't test for using `selectedIndex`.
MalphasWats
A: 

Code to get a vaiable columnName from the SELECT box called layerDetails.styleColumn (SELECT tag has same name and Id), that works across ALL browsers ...

var columnName = document.getElementsByName('layerDetails.styleColumn')[0].value;
if ( columnName == null || columnName == '' )
  {
  columnName = document.getElementById('layerDetails.styleColumn').value;
  }

if ( columnName == null || columnName == '' )
  {
  var select = document.getElementById('layerDetails.styleColumn');
  columnName= select.options[select.selectedIndex].value;
  if ( columnName == null || columnName == '' )
    {
    columnName= select.options[select.selectedIndex].text;
    }
  }
Ro