469,349 Members | 1,492 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,349 developers. It's quick & easy.

selectedIndex not functioning properly..?..

Hello.

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)
{
radio_buttons[i].onclick=removeRadioOpts;
}
else
{
radio_buttons[i].onclick=addRadioOpts;
}
}

var chkbox_sel = document.getElementById("checkchoice");
chkbox_sel.onclick=addCheckOpts;
}

....

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

....
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")
{
return true;
}

error.val=4;
return false;
}
....

Hopefully you can follow this..

--
Message posted via WebmasterKB.com
http://www.webmasterkb.com/Uwe/Forum...cript/200810/1

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

}
Here sel_opt holds a reference to the selected HTMLOptionElement
object.
>
...
* 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);
</script>

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.
<http://www.jibbering.com/faq/>

HTH,
/sasuke
Oct 2 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Nikhil Patel | last post: by
3 posts views Thread by Nathan Sokalski | last post: by
6 posts views Thread by Nathan Sokalski | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.