Solved

Jquery and html select tag

Posted on 2011-09-10
12
335 Views
Last Modified: 2012-05-12
Does anyone have an example of how to use a select (html form tag) that when something is selected, it will show or hide a div base upon what is selected?

I don't have any examples as I'm just getting started and can't find anything when I Google.

Basically I want to control (show / hide) a div with a select form tag.

Any help would be greatly appreciated.
0
Comment
Question by:jasch2244
  • 5
  • 5
  • 2
12 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 36517577
check this page : http://jsfiddle.net/JQLAx/

$("select").change(function() {
    if( $(this).val() == 0 ) {
        $(".mydiv").hide();
    }
    else if( $(this).val() == 1 ) {
        $(".mydiv").show();
    }        
})

Open in new window

<div class="mydiv">HELLO</div>
<br />
<select>
    <option value="">Select something</option>
    <option value="0">Hide Div</option>
    <option value="1">Show Div</option>
</select>

Open in new window

0
 
LVL 1

Author Comment

by:jasch2244
ID: 36517721
Ehh more like this... but I want it to animate (this is javascript version) but I need jquery to make it animate, fade etc.

Any thoughts on what I have thus far?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">

<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<title>Untitled Page</title>
		<script type="text/javascript"><!--
	var lastDiv = "";
	function showDiv(divName) {
	// hide last div
	if (lastDiv) {
		document.getElementById(lastDiv).className = "hiddenDiv";
	}
	//if value of the box is not nothing and an object with that name exists, then change the class
	if (divName && document.getElementById(divName)) {
		document.getElementById(divName).className = "visibleDiv";
		lastDiv = divName;
	}
}
//-->
</script>
		<style type="text/css" media="screen">
		<!--
.hiddenDiv {
	display: none;
	}
.visibleDiv {
	display: block;
	min-height:500px;
	border: 1px grey solid;
	width: 450px;
	background: -moz-linear-gradient(top, rgba(0,0,0,0.65) 0%, rgba(252,252,252,0) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.65)), color-stop(100%,rgba(252,252,252,0))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(252,252,252,0) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(252,252,252,0) 100%); /* Opera11.10+ */
background: -ms-linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(252,252,252,0) 100%); /* IE10+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a6000000', endColorstr='#00fcfcfc',GradientType=0 ); /* IE6-9 */
background: linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(252,252,252,0) 100%); /* W3C */
	}
-->
		</style>
	</head>

	<body bgcolor="#ffffff">
		<form id="FormName" action="blah.php" method="get" name="FormName">
			<select name="selectName" size="1" onchange="showDiv(this.value);">
				<option value="">Choose One...</option>
				<option value="one">first</option>
				<option value="two">second</option>
				<option value="three">third</option>
			</select>
		</form>
		<p id="one" class="hiddenDiv">This is paragraph 1.</p>
		<p id="two" class="hiddenDiv">This is paragraph 2.</p>
		<p id="three" class="hiddenDiv">This is paragraph 3.</p>		
	</body>

</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 36517727
>Basically I want to control (show / hide) a div with a select form tag.

and now :

>but I need jquery to make it animate, fade etc.

0
 
LVL 1

Author Comment

by:jasch2244
ID: 36517737
Yes most certainly
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 36517843
try:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<title>Untitled Page</title>
		<style type="text/css" media="screen">
		<!--
.hiddenDiv {
	display: none;
	}
.visibleDiv {
	display: block;
	min-height:500px;
	border: 1px grey solid;
	width: 450px;
	background: -moz-linear-gradient(top, rgba(0,0,0,0.65) 0%, rgba(252,252,252,0) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.65)), color-stop(100%,rgba(252,252,252,0))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(252,252,252,0) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(252,252,252,0) 100%); /* Opera11.10+ */
background: -ms-linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(252,252,252,0) 100%); /* IE10+ */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#a6000000', endColorstr='#00fcfcfc',GradientType=0 ); /* IE6-9 */
background: linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(252,252,252,0) 100%); /* W3C */
	}
-->
		</style>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$( function(){
	$('#selectName').bind('change',function(){
		var self=this;
		if(!$('.hiddenDiv:visible').size())
			$('#'+self.value).fadeIn('slow');
		else
			$('.hiddenDiv:visible').fadeOut('slow',function(){
				$('#'+self.value).fadeIn('slow');		
			});
	});
});
</script>
	</head>

	<body bgcolor="#ffffff">
		<form id="FormName" action="blah.php" method="get" name="FormName">
			<select name="selectName" id="selectName" size="1" >
				<option value="">Choose One...</option>
				<option value="one">first</option>
				<option value="two">second</option>
				<option value="three">third</option>
			</select>
		</form>
		<p id="one" class="hiddenDiv">This is paragraph 1.</p>
		<p id="two" class="hiddenDiv">This is paragraph 2.</p>
		<p id="three" class="hiddenDiv">This is paragraph 3.</p>		
	</body>

</html>

Open in new window

0
 
LVL 1

Author Comment

by:jasch2244
ID: 36517898
Yes exactly but.. the css does not work... does it have something to do with the DOM or something?
Nice solution
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 82

Assisted Solution

by:hielo
hielo earned 500 total points
ID: 36517945
move the hiddenDiv CSS definition so that it is AFTER the visibleDiv css definition, and in your DIVs, instead of just:
class="hiddenDiv"

use:
class="visibleDiv hiddenDiv"
0
 
LVL 1

Author Comment

by:jasch2244
ID: 36518021
Interesting worked in Firefox but not IE (no suprise) I'm happy to allot the credits any idea why IE wont take the code. I've updated it below.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<title>Untitled Page</title>
		<style type="text/css" media="screen">
		<!--
.visibleDiv {
	display: block;
	min-height:500px;
	border: 1px grey solid;
	width: 450px;
	background-color:#999999

	}
.hiddenDiv {
	display: none;
	}
	-->
		</style>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$( function(){
	$('#selectName').bind('change',function(){
		var self=this;
		if(!$('.hiddenDiv:visible').size())
			$('#'+self.value).fadeIn('slow');
		else
			$('.hiddenDiv:visible').fadeOut('slow',function(){
				$('#'+self.value).fadeIn('slow');		
			});
	});
});
</script>
	</head>

	<body bgcolor="#ffffff">
		<form id="FormName" action="blah.php" method="get" name="FormName">
			<select name="selectName" id="selectName" size="1" >
				<option value="">Choose One...</option>
				<option value="one">first</option>
				<option value="two">second</option>
				<option value="three">third</option>
			</select>
		</form>
		<p id="one" class="visibleDiv hiddenDiv">This is paragraph 1.</p>
		<p id="two" class="visibleDiv hiddenDiv">This is paragraph 2.</p>
		<p id="three" class="visibleDiv hiddenDiv">This is paragraph 3.</p>		
	</body>

</html>

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 36518060
well, I tested my suggestion earlier on IE 6  and it worked fine (it still does).  Sometime ago I was dealing with Opacity (as opposed to Gradient), and it turns out that for newer version of IE I had to ALSO add/declare the "-ms-filter" style of definition. Look at the way it was coded here:
http://stackoverflow.com/questions/2293910/css3-transparency-gradient

I can't test it because currently I have IE 6 only.
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 500 total points
ID: 36518062
Refer to the following:
http://reference.sitepoint.com/css/filter

specifically the "filters in IE 8" note/section.
0
 
LVL 82

Expert Comment

by:hielo
ID: 36518065
0
 
LVL 1

Author Closing Comment

by:jasch2244
ID: 36518080
Awesome!!!! Thank you so much
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now