getElementById('element').innerHTML query

S

soup_or_power

Hi
Sorry about the heading.
I have a table with td consisting of lists with <select></select>. When
I do a document.getElementById("element").innerHTML I don't see the
selected item. IOW, the innerHTML is not dynamic. Is there some way to
get the most recent selected without traversing through the list's
options.

Thank you
 
D

Dominic Tocci

Unless I don't understand the question, I'm not sure why you're using
innerHTML at all for this.
Try
document.FORMNAME.SELECTELEMENTNAME.options[document.FORMNAME.SELECTELEMENTNAME.selectedindex].value
replacing FORMNAME and SELECTEDELEMENTNAME with the appropriate names you've
chosen.
 
R

RobG

Hi
Sorry about the heading.
I have a table with td consisting of lists with <select></select>. When
I do a document.getElementById("element").innerHTML I don't see the
selected item. IOW, the innerHTML is not dynamic.

There is no specification for how innerHTML works, it is not a
standard. Many browsers implement it but only through reverse
engineering of Microsoft's implementation (or perhaps their own
version of what they thing Microsoft's should do). Therefore
expecting it to have any particular behaviour is wishful thinking,
though it has been implemented reasonably consistently as far as I
know.

The following is therefore pure speculation based on observation.

innerHTML reflects the current HTML of the element. The 'selected'
attribute of an option sets the *default* selected element, it does
not necessarily reflect the currently selected element.

The user selecting an option does not change the default selected
element, therefore the innerHTML of the select does not change. If
you change an element - say using DOM methods - then the innerHTML is
modified.
Is there some way to
get the most recent selected without traversing through the list's
options.

The value of the currently selected option, in some browsers, can be
accessed by getting the select's value. However, some browsers
don't support that and you must use the 'selectedIndex' property.
For multiple selects, you must iterate through the options to find
the selected items.

It would be nice if there was a 'getSelectedOptions' method that
returned an array of the selected options, but alas, there ain't.



<script type="text/javascript">

function showValue( b ) {
var f = b.form;
var sel = f.elements['sel0'];
alert(sel.value);
alert(sel[sel.selectedIndex].value);

}

</script>
<form action="">
<select name="sel0">
<option value="option 0" selected>Option 0</option>
<option value="option 1">Option 1</option>
<option value="option 2">Option 2</option>
<option value="option 3">Option 3</option>
</select>
<input type="button" value="Show value..." onclick="
showValue(this);
">
</form>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top