Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

javascript Option Box

Featured Replies

Hi,

 

I am having a little problem with a form i am trying to create. What i need it to do is when Company is selected it hides the freelance div and shows the company div and vice versa.

 

Here is what i have so far.

 

Javascript

function displayElements(number)
{
  switch(number)
  {
  case 1: document.getElementById("company").setAttribute("style", "visibility:visible");
     break;
  case 2: document.getElementById("freelance").setAttribute("style", "visibility:hidden");
     break;
  }
}

 

HTML

<select name="customerType" onclick="displayElements(this.value);">
<option value="0">Type?</option>
<option value="1">Freelancer</option>
<option value="2" >Company</option>
</select>
<div id="freelance" style="visibility:visible">
Forename <input type="text" name="forename" id="forename" />
Surname <input type="text" name="surname" id="surname" />
</div>
<div id="company" style="visibility:hidden">
Company Name <input type="text" name="companyName" id="companyName" />
</div>

Hi Thomas,

I'm just typing this in the editor so forgive me if there are any typos :)

Just before the </body> of your page put the following:

<style type="text/css">
.hidden {visibility:hidden;}
.visible {visibility:visible;}
</style>
<script type="text/javascript">
window.onload = function () {
	var dropDown = document.getElementById('customerType');
	document.getElementById('company').className = 'hidden';
	dropDown.onchange = function () {
		displayElements(this.options[this.selectedIndex].value);
	}

} 
function displayElements(index) {
switch(index){
case '1': document.getElementById("company").className = "hidden";
			document.getElementById("freelance").className = "visible";
	break;
case '2': document.getElementById("freelance").className = "hidden";
			document.getElementById("company").className = "visible";
	break;
}
}
</script>

and your html would look like this:

<select id="customerType" id="customerType">
	<option value="0">Type?</option>
	<option value="1">Freelancer</option>
	<option value="2" >Company</option>
</select>
<div id="freelance">
Forename <input type="text" name="forename" id="forename" />
Surname <input type="text" name="surname" id="surname" />
</div>
<div id="company">
Company Name <input type="text" name="companyName" id="companyName" />
</div>

This way you keep the javascript separate from your html.

Also notice, I've changed the class names of the divs to show/hide them instead of directly accessing the style property.

That keeps your js separate from your css :)

 

Anyway, it's not tested but should do the job

  • Author

Thanks a lot ElanMan.

 

That works fine, just out of interest was it possible to do it the way i was trying to with the onclick or onchange?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.