473,889 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 15642

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.sele ctedIndex].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.se lectedIndex;
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.le ngth;
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.nnwrot e in
news:wx******** *************** ******@40tude.n et:
>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:

"selectedIn dex 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.select edIndex<0? ==true/false

<script type="text/javascript">

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

<select
onmousedown="se en=this.selecte dIndex<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
8847
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 searching for. what i want to happen is that when a function is called a line of code will select a specific option in a combo box. something like: //excuse the dodgy syntax
3
11191
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, and depending on the entry made, the respective option in the list is to be highlighted. Somewhat like an autocolmplete... I have been able to achieve this, however, if the position/index of the selected option is beyond the SIZE of the SELECT list, then the OPTION positions itself at the bottom...
4
3216
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 are different stores on different pages that are selectable via a selection list. To avoid having to rewrite the list on each page, I moved it into a ..js file that produces it where called. Primary advantage is that when adding new pages, there only one option list to update.
3
18593
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 removes the option from my dropdown (although it still exists in the code). Is there an equivalent in IE? The reasoning behind this is that I want users to rank objects using a <select> for each place (see below), and to remove the choice of earlier objects from <select> drop-downs later in the...
8
8456
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> <title>FFinner.htm</title> <script type="text/javascript"> function clicks() { document.getElementById("t1").value =
3
3352
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 cgi language to accomplish this task, but it adds a lot of overhead and I would like to move the overhead to the remote client's PC, using javascript. Thank you for your assistance. Respectfully,
1
7346
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 it). This seems fairly simple but I can't seem to figure out how to do it. Does anybody know how to do this? Thanks. Puneet
1
2172
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 build admin pages for an ASP site. My problem is I have Categories and various Products in each Category. I'm trying to build a page to EDIT/UPDATE each product.I want to be able to change the Category that a product belongs to, but all I get in the drop-down menu is the one specific Category. ...
1
3483
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, then in some cases click a button and have the second set of 3 dropdown boxes be filled with the same values. thank you <head> <SCRIPT LANGUAGE="JavaScript" type="text/javascript"> var firstChoice2 = 0; var secondChoice2 = 0;
3
3251
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 have managed to get as far as I have from web searches for scripts and html and adapted them. I need to know the principles of how to disable certain options based on other selections within the form. I have attached my script and html below to help. For example of a customer selects (BASIC...
0
9810
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11198
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10442
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7993
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7150
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6029
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4647
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.