image onClick change SELECT roll-down OPTIONS

Dear JS Gurus

I have 2 images. One is called "for children" and the other is called "for adults". They look like tabs. Under that is a SELECT input box with a rolldown of categories. The categories are default to adults, when I click on the image "for children" I want the categories to change to another list, then if "for adults" is clicked, I want to change it back to the first list.

How can this be done in Javascript? Here's the options I want below.

Thanks!
<!-- If "for children" is clicked -->
<SELECT NAME="category">
  <OPTION>Clowns</OPTION>
  <OPTION>Facepainters</OPTION>
  <OPTION>Bouncy Castle Hire</OPTION>
</SELECT>
 
<!-- If "for adults" is clicked -->
<SELECT NAME="category">
  <OPTION>Live Bands</OPTION>
  <OPTION>Casino Hire</OPTION>
  <OPTION>Mobile Bar Hire</OPTION>
</SELECT>

Open in new window

LVL 11
tobzzzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ryangiglioCommented:
Hi tobzzz,

I would do this with CSS and the display property.

Give each select a unique ID, for example:

<!-- If "for children" is clicked -->
<SELECT NAME="category" id="children" style="display:none">
  <OPTION>Clowns</OPTION>
  <OPTION>Facepainters</OPTION>
  <OPTION>Bouncy Castle Hire</OPTION>
</SELECT>
 
<!-- If "for adults" is clicked -->
<SELECT NAME="category" id="adults" style="display:block">
  <OPTION>Live Bands</OPTION>
  <OPTION>Casino Hire</OPTION>
  <OPTION>Mobile Bar Hire</OPTION>
</SELECT>

Set the adult button to: onclick="changeMenu('adult')"
and the children button to: onclick="changeMenu('children')"

And include this javascript function
function changeMenu(clicked) {
  // If the children button is clicked
  if (clicked == "children") {
    // If children isn't currently being displayed
    if (document.getElementById('children').style.display == "none") {
      // Change it to display block - substitute "block" for whatever CSS style you see fit.
      document.getElementById('children').style.display = "block";
    }
  // Else - the adult button is clicked
  else {
    // If adult isn't currently being displayed
    if (document.getElementById('adult').style.display == "none") {
      // Change it to display block - substitute "block" for whatever CSS style you see fit.
      document.getElementById('adult').style.display = "block";
    }
  }
}

Open in new window

0
tobzzzAuthor Commented:
Hi ryangiglio

Thanks for advice. I've copied the code into a test doc. The problem is, when I click "children", the adult box is still there. I need to *only* show 1 select box, so essentially the boxes are swapping depending which button you click.

Also, there seems to be a javascript error of some kind running that code. I attach below how I've used your code.
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">
	function changeMenu(clicked) {
	  
	  // If the children button is clicked
	  if (clicked == "children") {
	    // If children isn't currently being displayed
	    if (document.getElementById('children').style.display == "none") {
	      // Change it to display block - substitute "block" for whatever CSS style you see fit.
	      document.getElementById('children').style.display = "block";
	    }
	  // Else - the adult button is clicked
	  else {
	    // If adult isn't currently being displayed
	    if (document.getElementById('adult').style.display == "none") {
	      // Change it to display block - substitute "block" for whatever CSS style you see fit.
	      document.getElementById('adult').style.display = "block";
	    }
	  }
	}
	}
</SCRIPT>
</HEAD>
<BODY>
<FORM ACTION="test.asp" METHOD="POST">
<P><input type="button" value="adults" onclick="changeMenu('adult')"></P>
<P><input type="button" value="kids" onclick="changeMenu('children')"></P>
<!-- If "for children" is clicked -->
<SELECT NAME="category" id="children" style="display:none">
  <OPTION>Clowns</OPTION>
  <OPTION>Facepainters</OPTION>
  <OPTION>Bouncy Castle Hire</OPTION>
</SELECT>
<!-- If "for adults" is clicked -->
<SELECT NAME="category" id="adults" style="display:block">
  <OPTION>Live Bands</OPTION>
  <OPTION>Casino Hire</OPTION>
  <OPTION>Mobile Bar Hire</OPTION>
</SELECT>
<P><input type="submit"></P>
</FORM>
/BODY>
</HTML>

Open in new window

0
ryangiglioCommented:
Your closing body tag is broken. that might be causing the error.

My mistake. I forgot to hide the other box as well.  Try this.


function changeMenu(clicked) {
  // If the children button is clicked
  if (clicked == "children") {
    // If children isn't currently being displayed
    if (document.getElementById('children').style.display == "none") {
      // Change it to display block - substitute "block" for whatever CSS style you see fit.
      document.getElementById('children').style.display = "block";
      document.getElementById('adult').style.display = "none";
    }
  // Else - the adult button is clicked
  else {
    // If adult isn't currently being displayed
    if (document.getElementById('adult').style.display == "none") {
      // Change it to display block - substitute "block" for whatever CSS style you see fit.
      document.getElementById('adult').style.display = "block";
      document.getElementById('children').style.display = "none";
    }
  }
}

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

ryangiglioCommented:
Oh, I found the error.  Again, my mistake.

The SELECT has an ID of "adults" but the code is using the ID of "adult"

This code works with no error! Sorry about that.
function changeMenu(clicked) {
          
          // If the children button is clicked
          if (clicked == "children") {
            // If children isn't currently being displayed
            if (document.getElementById('children').style.display == "none") {
              // Change it to display block - substitute "block" for whatever CSS style you see fit.
              document.getElementById('children').style.display = "block";
			  document.getElementById('adults').style.display = "none";
            }
          // Else - the adult button is clicked
          else {
            // If adult isn't currently being displayed
            if (document.getElementById('adults').style.display == "none") {
              // Change it to display block - substitute "block" for whatever CSS style you see fit.
              document.getElementById('adults').style.display = "block";
			  document.getElementById('children').style.display = "none";
            }
          }
        }
        }

Open in new window

0
tobzzzAuthor Commented:
I get this error for the function (see below). Sorry, I really don't know how to debug JS. I counted that you have as many open and close brackets so at a guess it looks good, but IE8 doesn't like it.

Message: Expected '}'
Line: 24
Char: 1

may be better to copy my code so you can see for yourself?
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">
	function changeMenu(clicked) {
	  // If the children button is clicked
	  if (clicked == "children") {
	    // If children isn't currently being displayed
	    if (document.getElementById('children').style.display == "none") {
	      // Change it to display block - substitute "block" for whatever CSS style you see fit.
	      document.getElementById('children').style.display = "block";
	      document.getElementById('adult').style.display = "none";
	    }
	  // Else - the adult button is clicked
	  else {
	    // If adult isn't currently being displayed
	    if (document.getElementById('adult').style.display == "none") {
	      // Change it to display block - substitute "block" for whatever CSS style you see fit.
	      document.getElementById('adult').style.display = "block";
	      document.getElementById('children').style.display = "none";
	    }
	  }
	}
</SCRIPT>
 
</HEAD>
 
<BODY>
 
<FORM ACTION="test.asp" METHOD="POST">
 
<P><input type="button" value="adults" onclick="changeMenu('adult')"></P>
 
<P><input type="button" value="kids" onclick="changeMenu('children')"></P>
 
<!-- If "for children" is clicked -->
<SELECT NAME="category" id="children" style="display:none">
  <OPTION>Clowns</OPTION>
  <OPTION>Facepainters</OPTION>
  <OPTION>Bouncy Castle Hire</OPTION>
</SELECT>
 
<!-- If "for adults" is clicked -->
<SELECT NAME="category" id="adults" style="display:block">
  <OPTION>Live Bands</OPTION>
  <OPTION>Casino Hire</OPTION>
  <OPTION>Mobile Bar Hire</OPTION>
</SELECT>
 
<P><input type="submit"></P>
	
</FORM>
 
</BODY>
</HTML>

Open in new window

0
ryangiglioCommented:
I apologize for spamming your thread.  I fixed the code you were working with.  There was a } missing after the first IF

This definitely works. Trust me :)
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">
        function changeMenu(clicked) {
          // If the children button is clicked
          if (clicked == "children") {
            // If children isn't currently being displayed
            if (document.getElementById('children').style.display == "none") {
              // Change it to display block - substitute "block" for whatever CSS style you see fit.
              document.getElementById('children').style.display = "block";
              document.getElementById('adults').style.display = "none";
            }
          }
          // Else - the adult button is clicked
          else {
            // If adult isn't currently being displayed
            if (document.getElementById('adults').style.display == "none") {
              // Change it to display block - substitute "block" for whatever CSS style you see fit.
              document.getElementById('adults').style.display = "block";
              document.getElementById('children').style.display = "none";
            }
          }
        }
</SCRIPT>
 
</HEAD>
 
<BODY>
 
<FORM ACTION="test.asp" METHOD="POST">
 
<P><input type="button" value="adults" onClick="changeMenu('adults')"></P>
 
<P><input type="button" value="kids" onClick="changeMenu('children')"></P>
 
<!-- If "for children" is clicked -->
<SELECT NAME="category" id="children" style="display:none">
  <OPTION>Clowns</OPTION>
  <OPTION>Facepainters</OPTION>
  <OPTION>Bouncy Castle Hire</OPTION>
</SELECT>
 
<!-- If "for adults" is clicked -->
<SELECT NAME="category" id="adults" style="display:block">
  <OPTION>Live Bands</OPTION>
  <OPTION>Casino Hire</OPTION>
  <OPTION>Mobile Bar Hire</OPTION>
</SELECT>
 
<P><input type="submit"></P>
        
</FORM>
 
</BODY>
</HTML>

Open in new window

0
tobzzzAuthor Commented:
That does the hiding and revealing perfectly, good stuff.

You're going to hate me, but..... even though you've hidden the SELECT input, it still passes through the category. So when I response.write the form variable I see
"Clowns, Live Bands" :-(

It doesn't solve my problem unfortunately. Any further thoughts how to get around this?

Thanks for help so far...
0
ryangiglioCommented:
There's two ways to solve that.

The first would be to make each dropdown have a unique name, such as

<SELECT NAME="children" id="children" style="display:none">
  <OPTION>Clowns</OPTION>
  <OPTION>Facepainters</OPTION>
  <OPTION>Bouncy Castle Hire</OPTION>
</SELECT>
 
<!-- If "for adults" is clicked -->
<SELECT NAME="adults" id="adults" style="display:block">
  <OPTION>Live Bands</OPTION>
  <OPTION>Casino Hire</OPTION>
  <OPTION>Mobile Bar Hire</OPTION>
</SELECT>

If that's not an option because of the way your processing page works, you're going to have to get a little more complex and use the javascript replaceChild() function to swap out the children and adults dropdowns.  If that's the route you need to go I can show you how that works.

0
tobzzzAuthor Commented:
Unfortunately I cannot rename the select input because when the code is used "in real life" it is for a search in the front end, and in the secure back-end it is used to assign the categories etc. basically it's used in lots of places and the codes already written :(

I've added a further 100 points on the question for it.
0
ryangiglioCommented:
Replace the old javascript function with this one

What it does is create a new "select" element and replace the old one with it.  That was there's only ever one at a time on the page.

you also don't need the style="display:block" in the select tag anymore.
function changeMenu(clicked) {
          if (clicked == "children") {
            if (document.getElementById('children') == null) {
				var childDropdown = document.createElement("select");
				childDropdown.setAttribute("id", "children");
				childDropdown.setAttribute("name", "category");
				
				var clowns = document.createElement("option");
				clowns.innerHTML = "Clowns";
				childDropdown.appendChild(clowns);
				
				var facepainters = document.createElement("option");
				facepainters.innerHTML = "FacePainters";
				childDropdown.appendChild(facepainters);
				
				var bouncyCastleHire = document.createElement("option");
				bouncyCastleHire.innerHTML = "Bouncy Castle Hire";
				childDropdown.appendChild(bouncyCastleHire);
				
				var adultDropdown = document.getElementById('adults');
				adultDropdown.parentNode.replaceChild(childDropdown, adultDropdown);
			} 
          }
          // Else - the adult button is clicked
          else {
            // If adult isn't currently being displayed
            if (document.getElementById('adults') == null) {
                var adultDropdown = document.createElement("select");
				adultDropdown.setAttribute("id", "adults");
				adultDropdown.setAttribute("name", "category");
				
				var liveBands = document.createElement("option");
				liveBands.innerHTML = "Live Bands";
				adultDropdown.appendChild(liveBands);
				
				var casinoHire = document.createElement("option");
				casinoHire.innerHTML = "Casino Hire";
				adultDropdown.appendChild(casinoHire);
				
				var mobileBarHire = document.createElement("option");
				mobileBarHire.innerHTML = "Mobile Bar Hire";
				adultDropdown.appendChild(mobileBarHire);
				
				var childDropdown = document.getElementById('children');
				childDropdown.parentNode.replaceChild(adultDropdown, childDropdown);
            }
          }
        }

Open in new window

0
tobzzzAuthor Commented:
Can you copy and paste what the BODY content should be like please, as it's not working for me.

Thanks!
0
ryangiglioCommented:
Sorry, I also forgot to mention you needed to delete the "child" select.
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript">
        function changeMenu(clicked) {
          if (clicked == "children") {
            if (document.getElementById('children') == null) {
				var childDropdown = document.createElement("select");
				childDropdown.setAttribute("id", "children");
				childDropdown.setAttribute("name", "category");
				
				var clowns = document.createElement("option");
				clowns.innerHTML = "Clowns";
				childDropdown.appendChild(clowns);
				
				var facepainters = document.createElement("option");
				facepainters.innerHTML = "FacePainters";
				childDropdown.appendChild(facepainters);
				
				var bouncyCastleHire = document.createElement("option");
				bouncyCastleHire.innerHTML = "Bouncy Castle Hire";
				childDropdown.appendChild(bouncyCastleHire);
				
				var adultDropdown = document.getElementById('adults');
				adultDropdown.parentNode.replaceChild(childDropdown, adultDropdown);
			} 
          }
          // Else - the adult button is clicked
          else {
            // If adult isn't currently being displayed
            if (document.getElementById('adults') == null) {
                var adultDropdown = document.createElement("select");
				adultDropdown.setAttribute("id", "adults");
				adultDropdown.setAttribute("name", "category");
				
				var liveBands = document.createElement("option");
				liveBands.innerHTML = "Live Bands";
				adultDropdown.appendChild(liveBands);
				
				var casinoHire = document.createElement("option");
				casinoHire.innerHTML = "Casino Hire";
				adultDropdown.appendChild(casinoHire);
				
				var mobileBarHire = document.createElement("option");
				mobileBarHire.innerHTML = "Mobile Bar Hire";
				adultDropdown.appendChild(mobileBarHire);
				
				var childDropdown = document.getElementById('children');
				childDropdown.parentNode.replaceChild(adultDropdown, childDropdown);
            }
          }
        }
</SCRIPT>
 
</HEAD>
 
<BODY>
 
<FORM ACTION="test.asp" METHOD="POST">
 
<P><input type="button" value="adults" onClick="changeMenu('adults')"></P>
 
<P><input type="button" value="kids" onClick="changeMenu('children')"></P>
 
<!-- If "for adults" is clicked -->
<SELECT NAME="category" id="adults">
  <OPTION>Live Bands</OPTION>
  <OPTION>Casino Hire</OPTION>
  <OPTION>Mobile Bar Hire</OPTION>
</SELECT>
 
<P><input type="submit"></P>
        
</FORM>
 
</BODY>
</HTML>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tobzzzAuthor Commented:
Thanks for persevering, that works really well. Great job :-)
0
tobzzzAuthor Commented:
One other question, if you don't mind...

How can I define the option value in the function?

E.g. see code below:
// Right now its...
var clowns = document.createElement("option");
clowns.innerHTML = "Clowns";
childDropdown.appendChild(clowns);
 
var facepainters = document.createElement("option");
facepainters.innerHTML = "FacePainters";
childDropdown.appendChild(facepainters);
 
gives:
<SELECT NAME="category" id="adults">
  <OPTION>Clowns</OPTION>
  <OPTION>Facepainters</OPTION>
</SELECT>
 
// How do I achieve this:
<SELECT NAME="category" id="adults">
  <OPTION VALUE="15">Clowns</OPTION>
  <OPTION VALUE="38">Facepainters</OPTION>
</SELECT>

Open in new window

0
ryangiglioCommented:
Not a problem.

You need to add a new line after you create each "option" element that sets its "value" attribute.

It looks like this:

option.setAttribute("value", "valueYouWant");

Here is an example.  I'm sure you can apply this to the rest of the options
var clowns = document.createElement("option");
clowns.innerHTML = "Clowns";
clowns.setAttribute("value", "15");				childDropdown.appendChild(clowns);

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.