...

View Full Version : Select.value (form) can be used?



mehere8
07-29-2011, 03:56 PM
I remember on a book it says that there is no 'value' property on 'select' element in the form.

However, I tested it on IE7/8/9, FF4, Select.value is unbelievably the item selected (i.e. Select.options[Select.seletecdIndex])

Does it also work on Safari Chrome or older versions of IE and FF? Is it a good practice?

Philip M
07-29-2011, 05:22 PM
I have no idea why your book should say that there is no value property of a select list option. Rubbish! All browsers support it - at least from IE5 onwards.



<select onchange = "showValue(this)">
<option selected value=""> Choose A Fruit</option>
<option value='Mango'> Mango </option>
<option value='Apple'> Apple </option>
<option value='Sunkist'> Sunkist </option>
<option value='Watermelon'> Watermelon </option>
<option value='Papaya'> Papaya </option>
</select>


<script type = "text/javascript">
function showValue(selectbox) {
var val = selectbox.value;
alert ("Option value is " + val);
}
</script>

The selected index is required only if the text of the option is to be captured, not the value.


<script type = "text/javascript">

function showValue(selectbox) {
var si = selectbox.selectedIndex;
var txt = selectbox.options[si].text;
alert ("Option text is " + txt)
}

</script>

This simple example may be helpful to you:-


<form name = "f">
<select name = "s"><option></option><option></option><option></option></select><br>
<input type = "button" value = "Populate Options" onclick = "populate()">

<script type = "text/javascript">
function populate() {
var d = document.f.s;
d.options[0].value = "1"; d.options[0].text = "One";
d.options[1].value = "2"; d.options[1].text = "Two";
d.options[2].value = "3"; d.options[2].text = "Three";
}
</script>

The older I grow the more I distrust the familiar doctrine that age brings wisdom. - H.L. Mencken 1880-1956, American Editor, Author, Critic, Humorist

mehere8
07-29-2011, 05:59 PM
so it is.

actually the book is JavaScript the Definitive Guide 5th. the following is what it says about select element:

In some ways, a select-multiple element is like a set of checkbox elements, and a select-one element is like a set of radio elements. The Select element differs from the toggle-button elements in that a single Select element represents an entire set of options. These options are specified in HTML with the <option> tag, and they are represented in JavaScript by Option objects stored in the options[] array of the Select element. Because a Select element represents a set of choices, it does not have a value property, as all other form elements do. Instead, as I'll discuss shortly, each Option object contained by the Select element defines a value property.

mrhoo
07-29-2011, 10:00 PM
One thing to watch out for-if the selected option does not have a value,

all browsers except IE before #9 return the selected option's text.

Older IE's return the empty string in that case.

devnull69
07-29-2011, 11:21 PM
I think this is what the Guide really WANTED to say


Because a Select element represents a set of choices, it does not have a value ATTRIBUTE, as all other form elements do. Instead, as I'll discuss shortly, each Option object contained by the Select element defines a value ATTRIBUTE. The select element always has a value PROPERTY which represents the value of the selected option in a select-one element and the value of the first selected option in a select-multiple element.

Dormilich
07-30-2011, 12:38 AM
if you’re not sure, whether a property exists, consult the DOM (http://www.w3.org/TR/DOM-Level-3-Core/idl-definitions.html) or the HTMLDOM (http://www.w3.org/TR/2000/WD-DOM-Level-2-HTML-20001113/idl-definitions.html) Specs.

Old Pedant
07-30-2011, 11:26 PM
Actually, folks, this "myth" isn't a myth, at all.

Netscape 4.7 (and earlier) indeed did *NOT* support the .value property of a <select>, at all.

So those of use who grew up trying to support MSIE 4 and Netscape 4 learned to do sel.options[sel.selectedIndex].value even though MSIE 4 *DID* support the .value property.

How's that for a turn of events? MSIE had it "right" before Netscape.

Now, it's possible that NS 4.7 was an aberration. Maybe NS 4.1 did support it. That I don't remember for sure. But I do remember the frustration with NS 4.7.

rnd me
07-31-2011, 10:10 PM
one monkey-wrench to throw into the mix: the "multiple" attrib. selecting more that one option mean that both .value and .selectedIndex are useless.

also, you can even set select.value, and so long as the sting you set it to is a valid option value, it automagically selects the proper index. pretty cool, and relatively under-used...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum