471,108 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,108 software developers and data experts.

getElementById('element').innerHTML query

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

Jul 23 '05 #1
3 5122
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.

<so***********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Jul 23 '05 #2
so***********@yahoo.com wrote:
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>


--
Rob
Jul 23 '05 #3

Do it through the FORM of the select tag, not the table, and as the other
fellas already said, use .selectedIndex.

Danny

On Fri, 01 Jul 2005 12:33:23 -0700, <so***********@yahoo.com> wrote:
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


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dave Hammond | last post: by
9 posts views Thread by s_m_b | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.