selectedIndex not functioning properly..?..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LayneMitch via WebmasterKB.com

    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.selecte dIndex];

    is working...'sel_ opt' has a value of 'option'..so I'm guessing that it's
    defined..becaus e 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.getEle mentById("my_bu tton");
    the_button.oncl ick=function(){ return checkFormFields ();};

    var radio_buttons = document.getEle mentsByName("be auty");
    for (var i=0; i < radio_buttons.l ength; i++)
    {

    if (i 0)
    {
    radio_buttons[i].onclick=remove RadioOpts;
    }
    else
    {
    radio_buttons[i].onclick=addRad ioOpts;
    }
    }

    var chkbox_sel = document.getEle mentById("check choice");
    chkbox_sel.oncl ick=addCheckOpt s;
    }

    ....

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

    ....
    correct=checkSe lOpts(sel_opt, error);

    if (!correct)
    {
    error_mes += errors[error.val] + '<br>';
    sel_opt.focus() ;
    }
    ....
    ....
    function checkSelOpts(ci ty_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


  • sasuke

    #2
    Re: selectedIndex not functioning properly..?..

    On Oct 1, 8:20 pm, "LayneMitch via WebmasterKB.com " <u39402@uwe>
    wrote:
    function checkFormFields ()
    {
      .....
      var sel_opt = the_sel.options[the_sel.selecte dIndex];
     ....
    >
    }
    Here sel_opt holds a reference to the selected HTMLOptionEleme nt
    object.
    >
    ...
      correct=checkSe lOpts(sel_opt, error);
    >
      if (!correct)
      {
        error_mes += errors[error.val] + '<br>';
        sel_opt.focus() ;
      }
    ...
    ...
    function checkSelOpts(ci ty_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.c om/faq/>

    HTH,
    /sasuke

    Comment

    Working...