472,352 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

see if an option's select is selected

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

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: mr_burns | last post by:
hi, using javascript, how do i select an option in a combo box? i have tried looking on the internet but dont really know what i should be...
3
by: Rithish Saralaya | last post by:
Is there a way to scroll a selected OPTION into view? I have a combination of text box and SELECT list. A user can key in text in the text box,...
4
by: Mark Kolber | last post by:
I did a little searching on this but couldn't find an answer... On my website, I have a section of stories (www.midlifeflight.com/stories) There...
3
by: Iain Hallam | last post by:
Hi. I've been using display:none on the style property of some <option> elements in my forms, which works fine with Mozilla - as expected it...
8
by: McKirahan | last post by:
Firefox does not reflect selected option via innerHTML How do I get Firefox to reflect selected option values? <html> <head>...
3
by: ANTISPAM_garycnew_ANTISPAM | last post by:
What is the simplest way to retain the last option value selected in an html select object using javascript? I am currently using a server-side...
1
by: puneet.bansal | last post by:
Hi, I want to know the index of the option that a user clicks on in a multiple-select object (regardless of whether he selected or deselected...
1
by: ollielaroo | last post by:
Hi guys, Firstly I did do a search for this one first but I couldn't find anything related in this forum. I am using Dreamweaver MX and trying to...
1
by: bytesFTW99 | last post by:
I have been struggling with this for some time can anyone help out? just trying to have 3 dropdown boxes that fill depending on what is selected,...
3
by: Venturini | last post by:
I am trying to put together a web page where the customer makes choices of products and is then given a total. I am extremely new to Javascript and...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.