enable - disable <select>.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Bedford

    enable - disable <select>.

    I've this code:

    function checkdate(FormS ubmit){
    alert(document. getElementById( 'Mois').value);
    if(eval(documen t.getElementByI d('Mois'))>0 &&
    eval(document.g etElementById(' Annee'))>0){
    document.forms['FormSubmit'].getElementByID ('SELECTCONSTRU CTOR').disabled
    = false; //can't reach the control here
    }
    else
    {
    document.forms['FormSubmit'].getElementByID ('SELECTCONSTRU CTOR').disabled
    = true; //can't reach the control here
    }
    }
    ....

    <form name="FormSubmi t" method="post" action="">
    ....
    <select name="Mois" onChange=checkd ate(this.form); >
    ....
    <select name="Annee" onChange=checkd ate(this.form); >
    ....
    <select name="SELECTCON STRUCTOR" disabled>

    Now, what I would like, is to enable the SELECTCONSTRUCT OR if month and year
    are selected (I mean different to 0).

    I can't enable or disable. In fact I can't reach the control in the
    checkdate function...

    How can I enable/disable SELECTCONSTRUCT OR when my 2 fields are selected ???

    BoB


  • Martin Honnen

    #2
    Re: enable - disable &lt;select&g t;.



    Bob Bedford wrote:
    [color=blue]
    > I've this code:
    >
    > function checkdate(FormS ubmit){
    > alert(document. getElementById( 'Mois').value);
    > if(eval(documen t.getElementByI d('Mois'))>0 &&
    > eval(document.g etElementById(' Annee'))>0){
    > document.forms['FormSubmit'].getElementByID ('SELECTCONSTRU CTOR').disabled
    > = false; //can't reach the control here
    > }
    > else
    > {
    > document.forms['FormSubmit'].getElementByID ('SELECTCONSTRU CTOR').disabled
    > = true; //can't reach the control here
    > }
    > }
    > ...
    >
    > <form name="FormSubmi t" method="post" action="">
    > ...
    > <select name="Mois" onChange=checkd ate(this.form); >
    > ...
    > <select name="Annee" onChange=checkd ate(this.form); >
    > ...
    > <select name="SELECTCON STRUCTOR" disabled>
    >
    > Now, what I would like, is to enable the SELECTCONSTRUCT OR if month and year
    > are selected (I mean different to 0).
    >
    > I can't enable or disable. In fact I can't reach the control in the
    > checkdate function...
    >
    > How can I enable/disable SELECTCONSTRUCT OR when my 2 fields are selected ???[/color]

    Forget about eval, you don't need it.
    If you have two select elements in a form container then simply pass the
    form to your function as you do e.g.
    <select onchange="check date(this.form) ;"
    and then in
    function checkdate (formObject) {
    var select1 = form.elements.M ois;
    var select2 = form.elements.S ELECTCONSTRUCTO R;
    ...
    if (...) {
    select2.disable d = false;
    }
    }

    --

    Martin Honnen


    Comment

    • HikksNotAtHome

      #3
      Re: enable - disable &lt;select&g t;.

      In article <3fe1caa5$0$791 $5402220f@news. sunrise.ch>, "Bob Bedford"
      <bedford1@YouKn owWhatToDohotma il.com> writes:
      [color=blue]
      >I've this code:[/color]



      Will explain how to get a reference to a select list.
      [color=blue]
      >function checkdate(FormS ubmit){
      > alert(document. getElementById( 'Mois').value);
      > if(eval(documen t.getElementByI d('Mois'))>0 &&
      >eval(document. getElementById( 'Annee'))>0){[/color]



      eval is not needed there, nor is the getElementById the most
      efficient/cross-browser way to do that:

      el1=document.fo rms['FormSubmit'].elements['Mois'];
      value= +el1.options[el1.selectedInd ex].value;
      el2=document.fo rms['FormSubmit'].elements['Annee'];
      value= +el2.options[el2.selectedInd ex].value;

      The + in the two lines converts the string value to a number.

      if ( el1>0 && el2>0){
      document.forms['FormSubmit'].elements['SELECTCONSTRUC TOR'].disabled=false ;
      }
      else{
      document.forms['FormSubmit'].elements['SELECTCONSTRUC TOR'].disabled=true;
      }


      --
      Randy

      Comment

      • Michael Winter

        #4
        Re: enable - disable &lt;select&g t;.

        Bob Bedford wrote on 18 Dec 2003 at Thu, 18 Dec 2003 15:41:46 GMT:
        [color=blue]
        > alert(document. getElementById( 'Mois').value);
        > if(eval(documen t.getElementByI d('Mois'))>0 &&
        > eval(document.g etElementById(' Annee'))>0){
        > document.forms['FormSubmit'].getElementByID ('SELECTCONSTRU CTOR'[/color]
        <snip>[color=blue]
        > document.forms['FormSubmit'].getElementByID ('SELECTCONSTRU CTOR'[/color]

        In addition to what Mr Honnen and Mr Hikks said, you would have
        trouble with the lines of code above.

        1) The latter two lines have the method name in incorrect case; the
        'D' in 'ID' should be lowercase.
        2) All of those lines have names as arguments. Names are *not* the
        same as IDs, and any browser that returns the correct element is
        broken (namely, IE but possibly others). The retrieval of named
        elements is accomplished using document.getEle mentsByName(). Be
        aware that this returns a collection returning all elements that
        match that name.

        As Mr Hikks said, the better way of accessing named form controls
        within a form element is through the use of the elements collection:

        document.forms['form name'].elements['element name']

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk")

        Comment

        Working...