473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

selectedIndex not functioning properly..?..


The following code has been shortened just to the main area. It's a form
validation problem. When I hit 'submit', the form is supposed to check if a
selection out of the drop down menu has been selected. If not then it's
suppose to generate an error message.

The question is with the variables and arguments passed to the function. I
already checked with Firebug, and this variable:

var sel_opt = the_sel.options[the_sel.selectedIndex];

is working...'sel_opt' has a value of 'option'..so I'm guessing that it's
defined..because if it wasn't then it would say 'undefined'.

If it is defined then why isn't my code checking if something was selected?

function attachHandlers(){

var the_button = document.getElementById("my_button");
the_button.onclick=function(){return checkFormFields();};

var radio_buttons = document.getElementsByName("beauty");
for (var i=0; i < radio_buttons.length; i++)

if (i 0)

var chkbox_sel = document.getElementById("checkchoice");


function checkFormFields()
var sel_opt = the_sel.options[the_sel.selectedIndex];

correct=checkSelOpts(sel_opt, error);

if (!correct)
error_mes += errors[error.val] + '<br>';
function checkSelOpts(city_opts, error){

if (city_opts!= -1 && city_opts.value !="none")
return true;

return false;

Hopefully you can follow this..

Message posted via WebmasterKB.com

Oct 1 '08 #1
1 1898
On Oct 1, 8:20*pm, "LayneMitch via WebmasterKB.com" <u39402@uwe>
function checkFormFields()
* .....
* var sel_opt = the_sel.options[the_sel.selectedIndex];

Here sel_opt holds a reference to the selected HTMLOptionElement
* correct=checkSelOpts(sel_opt, error);

* if (!correct)
* {
* * error_mes += errors[error.val] + '<br>';
* * sel_opt.focus();
* }
function checkSelOpts(city_opts, error){

*if (city_opts!= -1 && city_opts.value !="none")
Here you check whether the passed in `option' object equals -1? IMO,
checks on both value and the index selected are not really required;
only one would suffice the purpose. And whenever checking for
equality, make sure you use the strict equals operator === rather than
== to avoid miracles.

<script type="text/javascript">
alert("1" == 1);
alert("1" === 1);

Also make sure you throw up a test script or markup which one can test
to reproduce your problem rather than making others do the hard work.
Reading the newsgroup FAQ is a must.

Oct 2 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: ross kerr | last post by:
Hi all, I have a control that extends the ComboBox object. It updates the selected item based on what the user enters in the text area. In the OnLeave event of the combobox, the selected...
by: Nikhil Patel | last post by:
Hi all, I am using the standard System.Windows.Forms.ComboBox controls on a form. There are 5 or 6 comboboxes and their selection depends on each other. The comboboxes behave strangely probably...
by: Nathan Sokalski | last post by:
I am working on a simple user control composed of 3 DropDownLists that will be used to select Dates. The purpose of the control is to all the user to choose a date using dropdown lists but not need...
by: Nathan Sokalski | last post by:
I am writing a User Control that uses 3 DropDownLists. When I attempt to access the SelectedIndex property it incorrectly reports the value selected by the user. Why is this? Here is my code,...
by: Alec MacLean | last post by:
Hi, I have a couple of win forms where I am editing values that are stored in a SQL database. I'm using the listbox control to hold the data object each form interacts with. Each object is...
by: marcbb | last post by:
Hi all, I have a really strange problem working with Dropdownlists in a DataGrid. I'm trying to preselect some values from the DropDownlist for each row in the DataGrid, but it seems that...
by: januarynow | last post by:
Generally, my site contains javascripts (a couple of freebie counters plus some CPM (pay-per-impression) and CPC (pay-per-click) ads), from four different firms, but they are all suffering from the...
by: Academia | last post by:
I let the use modify the text of a combobox and then I replace the selected item with the new text (in Keyup event). But if he sets the Text property to an empty string ("") that sets the...
by: S_K | last post by:
Hi, I have a list of 6 DropDownList boxes, from DropDownList1 to DropDownList6, and I'm changing the SelectedIndex of each using a foreach loop as follows: foreach(PaymentReqDisplay...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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...
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...
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,...

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.