473,699 Members | 2,715 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 15631

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
8836
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
11176
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
3211
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
18584
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
8434
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
3342
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
7339
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
2163
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
3463
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
3242
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
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
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
9032
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...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
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
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.