Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I am attempting to show and hide divs based on a users drop down selection, what I want is when a user selects a sector say pubs the div pub will be displayed and the others hidden on the page.

The code is not working at the moment but I think its referenced correctly and cant find the issue.Any assistance would be great.

My divs are named like so
HTML
<div id="Pubs">
<div id="Councils">
<div id="Property">
<div id="Various">


My drop down list, HTML
HTML
<div id="Pub-Chains">


<pre lang="xml"><form>
              <p>
              <select id="dropdown" name="dropdown">
                  <option value="Pub-Chains" selected="selected"> Pub Chains </option>
                  <option value="Councils">Councils </option>
                  <option value="Property">Property </option>
                  <option value="Various">Various </option>
                  <option value="Universitys">Universitys </option>
              </select>
              </p>
          </form>


JS

XML
<script>

$('document').ready(function () {
var select = $( '#dropdown' );

function showTab( name ) {
  name = '#' + name;
  $( 'div' ).not( name ).hide();
  $( name ).show();
}

select.change( function() {
  showTab( $( this ).val() );
});

showTab( select.val() );


</script>
Posted

Not too sure if the event was properly bind. It might be, showTab( select.val() ); happens once at the start/load of the page and then never occurs again. Debugging would have made it clear.

Since you want this behaviour on every selection option change, you can try in other way to verify.
Something like following should do:
XML
 <form>
    <p>
    <select id="dropdown" name="dropdown" onchange="showTab();">
        <option value="Pub-Chains" selected="selected"> Pub Chains </option>
        <option value="Councils">Councils </option>
        <option value="Property">Property </option>
        <option value="Various">Various </option>
        <option value="Universitys">Universitys </option>
    </select>
    </p>

    <div id="Pubs-Chains">
    <div id="Councils">
    <div id="Property">
    <div id="Various">
</form>

JS:
XML
<script>

function showTab( ) {
  var select = $( '#dropdown' );
  $( 'div' ).not( select.val()).hide();
  $( name ).show();
}

</script>
 
Share this answer
 
It seems you have not closed the $('document').ready function properly with the }); like you're supposed to, and it works fine for me if you reference the drop down directly instead of using the 'select' variable.

Code in full:

HTML
<html>

<head>

	<title>Drop Down Example</title>

	<script type="text/javascript" src="jquery-1.8.3.min.js"></script> 

	<script>
		$(document).ready(function () {

			function showTab( name ) 
			{
  				name = '#' + name;

  				$('div').not(name).hide();

  				$(name).show();
			}
 
			$('#dropdown').change( function() {

  				showTab( $( this ).val() );
			});
 
			showTab( $('#dropdown').val() );
 
		});  // this line was missing 
	</script>

</head>

<body>

	<form>
              <p>
              <select id="dropdown" name="dropdown">
                  <option value="Pubs" selected="selected">Pubs </option>
                  <option value="Councils">Councils </option>
                  <option value="Property">Property </option>
                  <option value="Various">Various </option>
                  <option value="Universitys">Universitys </option>
              </select>
              </p>
         </form>


	<div id="Pubs">pubs</div>
	<div id="Councils">councils</div>
	<div id="Property">property</div>
	<div id="Various">various</div>
	<div id="Universitys">universitys</div>


</body>

</html>
 
Share this answer
 
v3
One question though youre code works on a standalone web page, but on my web page I have many Divs and the piece of code

JavaScript
$('div').not(name).hide();


Hides all the divs on the page and nothing is displayed.Is there a way of changing this?.
 
Share this answer
 
v2
Comments
Kiran Kumar 24-Oct-13 2:26am    
$('div id').not(name).hide();

I tried this and it worked, but the it does not hide the first called DIV.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900