By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

see if an option's select is selected

P: n/a
if I have a select with more options,
how I can know if is there an option selected;
is necessary a cycle? or is there an inner property?
Sep 12 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

artev wrote:
if I have a select with more options,
how I can know if is there an option selected;
is necessary a cycle? or is there an inner property?
With a select, there is always one option selected, therefore you don't
need to know if an option has been selected. However, if you really
meant to ask how to know which option is selected, consider the
following:

html:

<form>
<select name = "mySelect">
<option value = "1">one</option>
<option value = "2">two</option>
</select>
</form>

javascript:

document.forms[0].elements["mySelect"].selectedIndex;

The above should be able to tell you which option is currently
selected. If you want to get the value, then you could do the
following:

var selectElem = document.forms[0].elements["mySelect"];
var selectVal = selectElem[selectElem.selectedIndex].value;

Sep 12 '06 #2

P: n/a
ASM
artev a écrit :
if I have a select with more options,
how I can know if is there an option selected;
is necessary a cycle? or is there an inner property?
in my mind you can only get 1st selected option

<select onchange="var k = this.options.selectedIndex;
if(k>0) alert('choice = '+this.options[k].text);">

And to get all selected options (if mutiple)
I think you need a loop

<select onchange="var k = this.options.length;
var t = 'Your choices :\n';
for(var i=0;i<k;i++)
if(this.options[i].selected) t += this.options[i].text+'\n';
alert(t);">
--
Stephane Moriaux et son [moins] vieux Mac
Sep 12 '06 #3

P: n/a
With a select, there is always one option selected,

<form>
<select name = "mySelect" size="5">
<option value = "1">one</option>
<option value = "2">two</option>
</select>
</form>

Really in this, when I open the page noone option is selected;
only if I click on option I select it;
so I want use one code if that recognize if some option has been selected ;
not interesting that, but only if.
Sep 12 '06 #4

P: n/a
artev <ma***********@notspamm.nnwrote in
news:wx*****************************@40tude.net:
>With a select, there is always one option selected,

<form>
<select name = "mySelect" size="5">
<option value = "1">one</option>
<option value = "2">two</option>
</select>
</form>

Really in this, when I open the page noone option is selected...
Try adding this after the form:

<SCRIPT TYPE="text/javascript">
alert('selected element is ' +
document.forms[0].elements["mySelect"].selectedIndex);
</SCRIPT>

When the page loads, what does the alert say?
Sep 13 '06 #5

P: n/a
web.dev wrote:
artev wrote:
if I have a select with more options,
how I can know if is there an option selected;
is necessary a cycle? or is there an inner property?

With a select, there is always one option selected
Not necessarily. The W3C specifications do not state that one option
*must* be selected, only that:

"Zero or more choices may be pre-selected for the user."

HTML 4, section 17.6.1
<URL: http://www.w3.org/TR/html4/interact/...tml#idx-menu-2 >

It then goes on to state how that pre-selection might occur. Also, the
DOM 2 HTML specification includes:

"selectedIndex of type long
"The ordinal index of the selected option, starting from 0.
The value -1 is returned if no element is selected."

It seems unusual to specifically include a value for no selection if it
was thought that it should never occur (though it is by no means proof
of validity).
therefore you don't
need to know if an option has been selected. However, if you really
meant to ask how to know which option is selected, consider the
following:
The answer is that the selected index property can be use to determine
if an option is selected, but not to determine whether that is a
consequence of the user selecting the option, the browser deciding to
preselect a particular option (usually the first) or the HTML
specifying which option is to be preselected.

It might be possible to determine if no option is selected, but I
expect it isn't reliable. How can a user return to no selection without
resetting the form (if that functionality is provided for them at all)?

The best strategy is to ensure that one option (say the first or
zero-th) is always preselected in the HTML, then you know if the user
has selected some other option using:

if ( selectReference.selectedIndex ){
/* some option other than zero is selected */
}

or if you don't like that, try:

if ( selectReference.selectedIndex 0 ){
/* ... */
}

[...]

--
Rob

Sep 13 '06 #6

P: n/a
ASM
artev a écrit :
>With a select, there is always one option selected,

<form>
<select name = "mySelect" size="5">
<option value = "1">one</option>
<option value = "2">two</option>
</select>
</form>

Really in this, when I open the page noone option is selected;
only if I click on option I select it;
so I want use one code if that recognize if some option has been selected ;
not interesting that, but only if.
if no option selected the value of selectedIndex = '-1'

mySelect.selectedIndex<0? ==true/false

<script type="text/javascript">

function optSelected(selector) {
alert('selector : ' + selector + '\noption selected ? = ' +
!(document.forms[0][selector].selectedIndex<0) );
}
</script>
<a href="#" onclick="optSelected('mySelect');return false;">
mySelected ?
</a>
But, when selecting a new option you want to remember the previous choice :

<select
onmousedown="seen=this.selectedIndex<0? 'none' : this[selectedIndex].value;"
onchange="alert('previous option : '+seen);">
--
Stephane Moriaux et son [moins] vieux Mac
Sep 13 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.