Solved

JavaScript - show hide divs from select list

Posted on 2011-09-07
3
218 Views
Last Modified: 2012-05-12
Hi,
I can't work out what I am doing wrong here, also is there a better/shorter way of showing or hiding divs using jquery?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="application/javascript">

function show_payment_option(which_option){ 

	alert(which_option);
	
	
		if (which_option == 'credit_paypal'){
		   document.getElementById('paypal_option_details').style.display = 'block'; 
		   document.getElementById('cheque_option_details').style.display = 'none'; 
		   document.getElementById('direct_option_details').style.display = 'none';
		   document.getElementById('eway_option_details').style.display = 'none';
		   document.getElementById('pay_on_pickup_option_details').style.display = 'none'; 
		   document.getElementById('get_quote_option_details').style.display = 'none';		
		}else if (which_option == 'credit_eway'){
		   document.getElementById('paypal_option_details').style.display = 'none'; 
		   document.getElementById('cheque_option_details').style.display = 'none'; 
		   document.getElementById('direct_option_details').style.display = 'none'; 
		   document.getElementById('eway_option_details').style.display = 'block';
		   document.getElementById('pay_on_pickup_option_details').style.display = 'none'; 
		   document.getElementById('get_quote_option_details').style.display = 'none';		   
		}else if (which_option == 'cheque'){
		   document.getElementById('paypal_option_details').style.display = 'none'; 
		   document.getElementById('cheque_option_details').style.display = 'block'; 
		   document.getElementById('direct_option_details').style.display = 'none'; 
		   document.getElementById('eway_option_details').style.display = 'none'; 
		   document.getElementById('pay_on_pickup_option_details').style.display = 'none'; 
		   document.getElementById('get_quote_option_details').style.display = 'none';		
		}else if (which_option == 'direct'){
		   document.getElementById('paypal_option_details').style.display = 'none'; 
		   document.getElementById('cheque_option_details').style.display = 'none'; 
		   document.getElementById('direct_option_details').style.display = 'block'; 
		   document.getElementById('eway_option_details').style.display = 'none'; 
		   document.getElementById('pay_on_pickup_option_details').style.display = 'none'; 
		   document.getElementById('get_quote_option_details').style.display = 'none';		
		}else if (which_option == 'pay_on_pickup'){
		   document.getElementById('paypal_option_details').style.display = 'none'; 
		   document.getElementById('cheque_option_details').style.display = 'none'; 
		   document.getElementById('direct_option_details').style.display = 'none'; 
		   document.getElementById('eway_option_details').style.display = 'none'; 
		   document.getElementById('pay_on_pickup_option_details').style.display = 'block'; 
		   document.getElementById('get_quote_option_details').style.display = 'none';		
		}else if (which_option == 'pay_on_pickup'){
		   document.getElementById('paypal_option_details').style.display = 'none'; 
		   document.getElementById('cheque_option_details').style.display = 'none'; 
		   document.getElementById('direct_option_details').style.display = 'none'; 
		   document.getElementById('eway_option_details').style.display = 'none'; 
		   document.getElementById('pay_on_pickup_option_details').style.display = 'none'; 
		   document.getElementById('pay_on_pickup_option_details').style.display = 'block';		
		}else {
		   document.getElementById('paypal_option_details').style.display = 'none'; 
		   document.getElementById('cheque_option_details').style.display = 'none'; 
		   document.getElementById('direct_option_details').style.display = 'none'; 
		   document.getElementById('eway_option_details').style.display = 'none'; 
		   document.getElementById('pay_on_pickup_option_details').style.display = 'none'; 
		   document.getElementById('get_quote_option_details').style.display = 'none';		
		}

}

</script>




</head>

<body>




<select name="order_details[payment_option]" id="payment_option" onChange="show_payment_option(payment_option.value)">
<option value = "">Select Payment Option</option>
<option value="credit_paypal">Credit Card payment via PayPal.</option>
<option value="credit_eway">Credit Card - Master Card or Visa.</option>
<option value="cheque" >Cheque - must clear before goods dispatched</option>
<option value="direct" >Direct Deposit into our Bank Account </option>
<option value="get_quote">Make Enquiry/Get Quote</option>
<option value="pay_on_pickup" >Pay On Pickup</option>
</select>


<div id="paypal_option_details">Pay Pay Options Details</div>
<div id="cheque_option_details">Cheque Options Details</div>
<div id="direct_option_details">Direct Deposit Details</div>
<div id="eway_option_details">Pay Pay Options Details</div>
<div id="pay_on_pickup_option_details">Cheque Options Details</div>
<div id="get_quote_option_details">Direct Deposit Details</div>


</body>
</html>

Open in new window

0
Comment
Question by:sabecs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36500656
Very simple. Look at below code. You can test below code at

http://jsfiddle.net/E2qS6/1/
<!DOCTYPE html>
<html>
<head>
<title>Kiran Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
 $('#payment_option').change(function(){
  show_payment_option();
  });
  hide_divs();
});

function show_payment_option(){ 

   var which_option = $('#payment_option').val();
   hide_divs();
    
    switch (which_option) {
      case "credit_paypal":
        $('#paypal_option_details').show();
        break;
      case "credit_eway":
        $('#eway_option_details').show();
        break;
      case "cheque":
        $('#cheque_option_details').show();
        break;
      case "direct":
        $('#direct_option_details').show();
        break;
      case "credit_paypal":
        $('#paypal_option_details').show();
        break;
      case "pay_on_pickup":
        $('#pay_on_pickup_option_details').show();
        break;
    }

}

function hide_divs()
{
  $('#paypal_option_details').hide();
 $('#cheque_option_details').hide();
 $('#direct_option_details').hide();
 $('#eway_option_details').hide();
 $('#pay_on_pickup_option_details').hide();
 $('#get_quote_option_details').hide();
}

</script>
</head>
<body>
  <form>
  <select name="order_details[payment_option]" id="payment_option">
<option value = "">Select Payment Option</option>
<option value="credit_paypal">Credit Card payment via PayPal.</option>
<option value="credit_eway">Credit Card - Master Card or Visa.</option>
<option value="cheque" >Cheque - must clear before goods dispatched</option>
<option value="direct" >Direct Deposit into our Bank Account </option>
<option value="get_quote">Make Enquiry/Get Quote</option>
<option value="pay_on_pickup" >Pay On Pickup</option>
</select>


<div id="paypal_option_details">Pay Pay Options Details</div>
<div id="cheque_option_details">Cheque Options Details</div>
<div id="direct_option_details">Direct Deposit Details</div>
<div id="eway_option_details">Pay Pay Options Details</div>
<div id="pay_on_pickup_option_details">Cheque Options Details</div>
<div id="get_quote_option_details">Direct Deposit Details</div>
</form>
</body>
</html>

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 36500662
try:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function(){
	$('.preview').hide();
	$('#payment_option').bind('change',function(){
		$('.preview').hide();
		$('#'+this.value).show();
	});	
});
</script>


</head>

<body>




<select name="order_details[payment_option]" id="payment_option">
<option value = "">Select Payment Option</option>
<option value="credit_paypal">Credit Card payment via PayPal.</option>
<option value="credit_eway">Credit Card - Master Card or Visa.</option>
<option value="cheque" >Cheque - must clear before goods dispatched</option>
<option value="direct" >Direct Deposit into our Bank Account </option>
<option value="get_quote">Make Enquiry/Get Quote</option>
<option value="pay_on_pickup" >Pay On Pickup</option>
</select>


<div class="preview" id="credit_paypal">Paypal Options Details</div>
<div class="preview" id="credit_eway">Pay E-Way Options Details</div>
<div class="preview" id="cheque">Cheque Options Details</div>
<div class="preview" id="direct">Direct Deposit Details</div>
<div class="preview" id="get_quote">Get Quote</div>
<div class="preview" id="pay_on_pickup">Pay on Pickup Options Details</div>


</body>
</html>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:sabecs
ID: 36500774
Thanks hielo, that fantastic.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

627 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