Failed to get selected value from list in IE 8

Unable to get selected value from list in IE 8

<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>

I get a value similar to the following

var myvalue= document.getElementById("fileName").value;
alert(myvalue);
+3
source share
6 answers
var select = document.getElementById("fileName");
var myvalue= select.options[select.selectedIndex].value;
alert(myvalue);

select.valueappears in the DOM specification , but IE has never been implemented. The property selectedIndexworks everywhere.

UPDATE: as anddoutoi points out in a comment on the original question, it is assumed that you are using a single element select.

+7
source
var selectElement = document.getElementById("selectElementId");
var selectedValue = selectElement.options[selectElement.selectedIndex].value;

This should be changed to:

var selectElement = document.getElementById("selectElementId");
var selectedValue = selectElement.options[selectElement.selectedIndex].text;

Changing .valueto .textsolves the problem and is compatible with all browsers.

+5
source

HTML. :

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>

<option>. :

<select id="fileName">
    <option id="my1Div" value="test1">test1</option>
    ...
    <option id="my5Div" value="test3">test3</option>
</select>
+4
source
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!

+1
source
var selectElement = document.getElementById("selectElementId");
var selectedValue = selectElement.options[selectElement.selectedIndex].value;
+1
source

Code to get a vaiable columnName from a SELECT window called layerDetails.styleColumn (the SELECT tag has the same name and identifier), which works in 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;
    }
  }
0
source

Source: https://habr.com/ru/post/1722680/


All Articles