Allowing 2 Select Menus to work together with Javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chunk1978
    New Member
    • Jan 2007
    • 224

    #1

    Allowing 2 Select Menus to work together with Javascript?

    hey everyone,

    i've been trying to solve this problem for 2 days straight, with no end in sight. i would greatly appreciate anyone's help.


    EXPLANATION: There are 3 Select Menus. The 1st and 2nd Select Menu are "printing options" for 4x6 and 5x7 prints respectively. the 3rd Select Menu holds an "Email" option that can be toggled (appear/reappear) by the 1st and 2nd Select Menus. So if a user selects prints from either 4x6 or 5x7 menu, the "Email" option should disappear so the user can't select it (after all, you can't really email prints right?). The goal is that if there are any 4x6 or 5x7 prints selected (separately or simultaneously) , the "Email" option will not be available.


    THE PROBLEM: both 4x6 and 5x7 Select Menus work separately, but getting them to work together is the trouble. for example: if you choose any 4x6 prints, the "Email" option disappears, then if you select "4x6 File (With Email Option!)", the "Email" option will reappear. the same goes for the 5x7 Select Menu if it is uses seperatly.

    however, if at first a user wants to have 4x6 and 5x7 prints done, the "Email" option disappears. but if the user changes his/her mind about the 5x7 prints, and changes that select menu back to "5x7 File (With Email Option)" while the 4x6 Select Menu is still selecting a print, the "Email" option will reappear! again, the goal is to have the "Email" option unavailable if either of the Select Menus have any prints selected.

    [HTML]
    <HTML>
    <HEAD>

    <SCRIPT LANGUAGE=JavaSc ript>

    function ToggleEmail4x6 (val)
    {
    if(val=="4x6_0" )
    {AddEmailToRece ving()}
    else {RemoveEmailFro mReceving()}
    }

    function ToggleEmail5x7 (val)
    {
    if(val=="5x7_0" )
    {AddEmailToRece ving()}
    else {RemoveEmailFro mReceving()}
    }

    function RemoveEmailFrom Receving()
    {
    if (document.IMAGE Form.recevingop tions.options[2].text == "Email")
    {document.IMAGE Form.recevingop tions.options[2] = null;}
    }

    function AddEmailToRecev ing()
    {
    if (document.IMAGE Form.recevingop tions.options[2].text != "Email")
    {
    var indexCounter;
    var recevingchoice = document.IMAGEF orm.recevingopt ions;
    var lastoption = new Option();
    recevingchoice. options[3] = lastoption;

    for (indexCounter = 3;indexCounter > 2; indexCounter--)
    {
    recevingchoice. options[indexCounter].text = recevingchoice. options[indexCounter - 1].text;
    recevingchoice. options[indexCounter].value = recevingchoice. options[indexCounter - 1].value;
    }

    var option = new Option("Email", 2);
    recevingchoice. options[2] = option;
    }
    }

    </SCRIPT>
    </HEAD>
    <BODY>

    <FORM NAME=IMAGEForm>
    <p><BR><label ></label>
    A) Please Select Prints </p>
    <p>
    <label>
    <select name=4x6printop tions onChange="Toggl eEmail4x6(this. value)">
    <option value="4x6_0">4 x6 File (With Email Option!)</option>
    <option value="4x6_1">O ne 4x6 Print</option>
    <option value="4x6_2">T wo 4x6 Prints</option>
    <option value="4x6_3">T hree 4x6 Prints</option>
    </select>
    </label>
    </p>
    <p>
    <label>
    <select name=5x7printop tions onChange="Toggl eEmail5x7(this. value)">
    <option value="5x7_0">5 x7 File (With Email Option!)</option>
    <option value="5x7_1">O ne 5x7 Print</option>
    <option value="5x7_2">T wo 5x7 Prints</option>
    <option value="5x7_3">T hree 5x7 Prints</option>
    </select>
    </label>
    </p>
    <p>&nbsp;</p>
    <p>B) Please Select your Receving Choice:<BR>
    <select name=recevingop tions size="4">
    <option value="0">
    <option value="1">Couri er
    <option value="2">Email
    <option value="3">Local Pick-Up
    </select>
    </p>
    </FORM>
    </BODY>
    </HTML>
    [/HTML]

    Edited by iam_clint: Please wrap your code in [ code] or [ html] tags. I did it for you this time.

    Thanks for anyone's help!
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    There are possibly a number of ways, but based on your code, this is one:
    Code:
    function ToggleEmail4x6 (val) {
     var 5x7val = document.IMAGEForm.5x7printoptions.value;
     if (val=="4x6_0" && 5x7val == "5x7_0") {
      AddEmailToReceving()
     } else { RemoveEmailFromReceving() }
    }
    The change to the code checks the 5x7 select too and makes sure that is also an email option. Change the ToggleEmail5x7 function too. Alternatively, combine the two into one function and on both onchange handlers call the same function and instead of passing this.value just find the selected values in the function - removes the need for two functions.

    If you don't understand anything, just post again.

    Comment

    • chunk1978
      New Member
      • Jan 2007
      • 224

      #3
      thanks for the reply

      Originally posted by acoder
      There are possibly a number of ways, but based on your code, this is one:
      i've tried the code you've written in the last post, but it doesn't work... oddly, since it makes sense in reading... i would ideally like to combine the ToggleEmail4x6 and ToggleEmail5x7, but my new code doesn't work either... forgive me if i'm missing something obvious, i've only bee really working with javascript for about 2 weeks.

      here is my new code (that doesn't work)... what is wrong with it? what optimizations could you suggest that would make sense to a novice coder like myself...

      Code:
      <HTML>
      <HEAD>
      
      <SCRIPT LANGUAGE=JavaScript>
      
      function ToggleEmail(val)
      	{
      	var 4x6val = document.IMAGEForm.4x6printoptions.value;
      	var 5x7val = document.IMAGEForm.5x7printoptions.value;
      	if (4x6val=="4x6_0" && 5x7val=="5x7_0") 
      	{
      	AddEmailToReceving()
       	} 
       	else {RemoveEmailFromReceving()}
      }
      	
      function RemoveEmailFromReceving() 
      	{
      	if (document.IMAGEForm.recevingoptions.options[2].text == "Email") 
      	{document.IMAGEForm.recevingoptions.options[2] = null;} 
      }
      
      function AddEmailToReceving() 
      	{
          if (document.IMAGEForm.recevingoptions.options[2].text != "Email") 
      	{
          var indexCounter;
          var recevingchoice = document.IMAGEForm.recevingoptions;
          var lastoption = new Option();
          recevingchoice.options[3] = lastoption;
      
          for (indexCounter = 3;indexCounter > 2; indexCounter--)
          {
          recevingchoice.options[indexCounter].text = recevingchoice.options[indexCounter - 1].text;
          recevingchoice.options[indexCounter].value = recevingchoice.options[indexCounter - 1].value;
          }
      
          var option = new Option("Email",2);
          recevingchoice.options[2] = option;
          }
      }
      
      </SCRIPT>
      </HEAD>
      <BODY>
      
      <FORM NAME=IMAGEForm>
        <p><BR><label></label>
        A) Please Select Prints </p>
        <p>
          <label>
          <select name=4x6printoptions onChange="ToggleEmail(this.value)">
            <option value="4x6_0">4x6 File (With Email Option!)</option>
            <option value="4x6_1">One 4x6 Print</option>
            <option value="4x6_2">Two 4x6 Prints</option>
            <option value="4x6_3">Three 4x6 Prints</option>
          </select>
          </label>
        </p>
        <p>
          <label>
          <select name=5x7printoptions onChange="ToggleEmail(this.value)">
            <option value="5x7_0">5x7 File (With Email Option!)</option>
            <option value="5x7_1">One 5x7 Print</option>
            <option value="5x7_2">Two 5x7 Prints</option>
            <option value="5x7_3">Three 5x7 Prints</option>
          </select>
          </label>    
        </p>
        <p>&nbsp;</p>
        <p>B) Please Select your Receving Choice:<BR>
            <select name=recevingoptions size="4">
              <option value="0">
              <option value="1">Courier
              <option value="2">Email
              <option value="3">Local Pick-Up 
            </select>
        </p>
        </FORM>
      </BODY>
      </HTML>

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        Try this out. good luck and reply if it works for you please.
        [HTML]
        <HTML>
        <HEAD>
        <SCRIPT LANGUAGE="JavaS cript">
        function ToggleEmail() {
        var eval = document.getEle mentById("4x6") .value;
        var evalb = document.getEle mentById("5x7") .value;
        if (eval=="4x6_0" && evalb=="5x7_0") {
        AddEmailToRecev ing()
        } else {
        RemoveEmailFrom Receving()
        }
        }

        function RemoveEmailFrom Receving() {
        if (document.IMAGE Form.recevingop tions.options[2].text == "Email")
        {document.IMAGE Form.recevingop tions.options[2] = null;}
        }
        function AddEmailToRecev ing() {
        if (document.IMAGE Form.recevingop tions.options[2].text != "Email") {
        var indexCounter;
        var recevingchoice = document.IMAGEF orm.recevingopt ions;
        var lastoption = new Option();
        recevingchoice. options[3] = lastoption;
        for (indexCounter = 3;indexCounter > 2; indexCounter--) {
        recevingchoice. options[indexCounter].text = recevingchoice. options[indexCounter - 1].text;
        recevingchoice. options[indexCounter].value = recevingchoice. options[indexCounter - 1].value;
        }
        var option = new Option("Email", 2);
        recevingchoice. options[2] = option;
        }
        }
        </SCRIPT>
        </HEAD>
        <BODY>
        <FORM NAME="IMAGEForm " ID="IMAGEForm" >
        <p><BR><label ></label>
        A) Please Select Prints </p>
        <p>
        <label>
        <select name="4x6printo ptions" id="4x6" onChange="Toggl eEmail()">
        <option value="4x6_0">4 x6 File (With Email Option!)</option>
        <option value="4x6_1">O ne 4x6 Print</option>
        <option value="4x6_2">T wo 4x6 Prints</option>
        <option value="4x6_3">T hree 4x6 Prints</option>
        </select>
        </label>
        </p>
        <p>
        <label>
        <select name="5x7printo ptions" id="5x7" onChange="Toggl eEmail()">
        <option value="5x7_0">5 x7 File (With Email Option!)</option>
        <option value="5x7_1">O ne 5x7 Print</option>
        <option value="5x7_2">T wo 5x7 Prints</option>
        <option value="5x7_3">T hree 5x7 Prints</option>
        </select>
        </label>
        </p>
        <p>&nbsp;</p>
        <p>B) Please Select your Receving Choice:<BR>
        <select name=recevingop tions size="4">
        <option value="0">
        <option value="1">Couri er
        <option value="2">Email
        <option value="3">Local Pick-Up
        </select>
        </p>
        </FORM>
        </BODY>
        </HTML>
        [/HTML]

        Comment

        • chunk1978
          New Member
          • Jan 2007
          • 224

          #5
          Originally posted by iam_clint
          Try this out. good luck and reply if it works for you please.
          nope, still doesn't work... :-/ but thank you for trying to help

          Comment

          • iam_clint
            Recognized Expert Top Contributor
            • Jul 2006
            • 1207

            #6
            then i am missing what your trying todo

            if user selects email in 1
            and then the user selects print in other

            email should not be available

            email should only be available if both are selected as email?

            Comment

            • chunk1978
              New Member
              • Jan 2007
              • 224

              #7
              Originally posted by iam_clint
              then i am missing what your trying todo

              if user selects email in 1
              and then the user selects print in other

              email should not be available

              email should only be available if both are selected as email?
              you've got it! :-) that is exactly my goal... i'm using dreamweaver and safari/firefox on a mac (but i don't that that's the reason why this code doesn't work, but i could be wrong)

              when i say "it's not working" i really mean it doesn't work... like nothing happens when a select menu is pulled down, and i assume that's because the javascript is getting hung-up on a syntax error in the ToggleEmail() function.

              Comment

              • iam_clint
                Recognized Expert Top Contributor
                • Jul 2006
                • 1207

                #8
                Code:
                <HTML>
                <HEAD>
                <SCRIPT LANGUAGE="JavaScript">
                function ToggleEmail() {
                 var eval = document.getElementById("4x6").value;
                 var evalb = document.getElementById("5x7").value;
                 if (eval=="4x6_0" && evalb=="5x7_0") {
                   AddEmailToReceving()
                  } else { 
                	  RemoveEmailFromReceving() 
                  }
                }
                 
                function RemoveEmailFromReceving() {
                 if (document.IMAGEForm.recevingoptions.options[2].text == "Email") 
                 {document.IMAGEForm.recevingoptions.options[2] = null;} 
                }
                function AddEmailToReceving() {
                 if (document.IMAGEForm.recevingoptions.options[2].text != "Email") {
                 var indexCounter;
                 var recevingchoice = document.IMAGEForm.recevingoptions;
                 var lastoption = new Option();
                 recevingchoice.options[3] = lastoption;
                 for (indexCounter = 3;indexCounter > 2; indexCounter--) {
                 recevingchoice.options[indexCounter].text = recevingchoice.options[indexCounter - 1].text;
                 recevingchoice.options[indexCounter].value = recevingchoice.options[indexCounter - 1].value;
                 }
                 var option = new Option("Email",2);
                 recevingchoice.options[2] = option;
                 }
                }
                </SCRIPT>
                </HEAD>
                <BODY>
                <FORM NAME="IMAGEForm" ID="IMAGEForm">
                  <p><BR><label></label>
                  A) Please Select Prints </p>
                  <p>
                 <label>
                 <select name="4x6printoptions" id="4x6" onChange="ToggleEmail()">
                   <option value="4x6_0">4x6 File (With Email Option!)</option>
                   <option value="4x6_1">One 4x6 Print</option>
                   <option value="4x6_2">Two 4x6 Prints</option>
                   <option value="4x6_3">Three 4x6 Prints</option>
                 </select>
                 </label>
                  </p>
                  <p>
                 <label>
                 <select name="5x7printoptions" id="5x7" onChange="ToggleEmail()">
                   <option value="5x7_0">5x7 File (With Email Option!)</option>
                   <option value="5x7_1">One 5x7 Print</option>
                   <option value="5x7_2">Two 5x7 Prints</option>
                   <option value="5x7_3">Three 5x7 Prints</option>
                 </select>
                 </label> 
                  </p>
                  <p>&nbsp;</p>
                  <p>B) Please Select your Receving Choice:<BR>
                   <select name=recevingoptions size="4">
                  <option value="0">
                  <option value="1">Courier
                  <option value="2">Email
                  <option value="3">Local Pick-Up 
                   </select>
                  </p>
                  </FORM>
                </BODY>
                </HTML>
                This code worked fine in firefox and ie6 for me... make sure you copy and paste the entire code i gave and not just the javascript.

                Comment

                • chunk1978
                  New Member
                  • Jan 2007
                  • 224

                  #9
                  IT WORKS!!! i figured it out... in the ToggleEmail() function, there was a getElementById, while there was no real id given in the HTML code... however i've tried various VAR names for the ToggleEmail() function, and it seems that javascript can be extremely tempremental when it comes to VARs and IDs that have numbers in them... (???)

                  anyway... the following script works:

                  [HTML]<HTML>
                  <HEAD>

                  <SCRIPT LANGUAGE=JavaSc ript>

                  function ToggleEmail() {
                  var ChoiceA = document.getEle mentById("4x6") .value;
                  var ChoiceB = document.getEle mentById("5x7") .value;
                  if (ChoiceA=="4x6_ 0" && ChoiceB=="5x7_0 ") {
                  AddEmailToRecev ing()
                  }
                  else{
                  RemoveEmailFrom Receving()
                  }
                  }

                  function RemoveEmailFrom Receving()
                  {
                  if (document.IMAGE Form.recevingop tions.options[2].text == "Email")
                  {document.IMAGE Form.recevingop tions.options[2] = null;}
                  }

                  function AddEmailToRecev ing()
                  {
                  if (document.IMAGE Form.recevingop tions.options[2].text != "Email")
                  {
                  var indexCounter;
                  var recevingchoice = document.IMAGEF orm.recevingopt ions;
                  var lastoption = new Option();
                  recevingchoice. options[3] = lastoption;

                  for (indexCounter = 3;indexCounter > 2; indexCounter--)
                  {
                  recevingchoice. options[indexCounter].text = recevingchoice. options[indexCounter - 1].text;
                  recevingchoice. options[indexCounter].value = recevingchoice. options[indexCounter - 1].value;
                  }

                  var option = new Option("Email", 2);
                  recevingchoice. options[2] = option;
                  }
                  }

                  </SCRIPT>
                  </HEAD>
                  <BODY>

                  <FORM NAME=IMAGEForm>
                  <p><BR><label ></label>
                  A) Please Select Prints </p>
                  <p>
                  <label>
                  <select name=4x6printop tions id=4x6 onChange="Toggl eEmail()">
                  <option value="4x6_0">4 x6 File (With Email Option!)</option>
                  <option value="4x6_1">O ne 4x6 Print</option>
                  <option value="4x6_2">T wo 4x6 Prints</option>
                  <option value="4x6_3">T hree 4x6 Prints</option>
                  </select>
                  </label>
                  </p>
                  <p>
                  <label>
                  <select name=5x7printop tions id=5x7 onChange="Toggl eEmail()">
                  <option value="5x7_0">5 x7 File (With Email Option!)</option>
                  <option value="5x7_1">O ne 5x7 Print</option>
                  <option value="5x7_2">T wo 5x7 Prints</option>
                  <option value="5x7_3">T hree 5x7 Prints</option>
                  </select>
                  </label>
                  </p>
                  <p>&nbsp;</p>
                  <p>B) Please Select your Receving Choice:<BR>
                  <select name=recevingop tions size="4">
                  <option value="0">
                  <option value="1">Couri er
                  <option value="2">Email
                  <option value="3">Local Pick-Up
                  </select>
                  </p>
                  </FORM>
                  </BODY>
                  </HTML>[/HTML]

                  Comment

                  • chunk1978
                    New Member
                    • Jan 2007
                    • 224

                    #10
                    thanks for all your help iam_clint! really appreciate it

                    Comment

                    Working...