Solved

Toggle display of DIV based on onSelect event handler

Posted on 2006-06-09
7
597 Views
Last Modified: 2010-08-05
Now I'm trying to toggle the display of DIVs based on onSelect event handler.

This is my javascript:

function showDiv(divID) {
if (document.getElementById(divID).style.display == 'block';)
{document.getElementById(divID).style.display = 'none';}
else
{document.getElementById(divID).style.display = 'block';}
}

And this is my HTML:

Type of payment account:
<select name="paymentAccountType" onchange="showDiv(this.value);" >
     <option></option>
     <option value="credit">Credit card</option>
     <option value="debit">Debit card</option>
     <option value="eft">EFT</option>
     </select>


<div id="credit" style="display:none;">Credit card</div>
<div id="debit" style="display:none;">Debit card</div>
<div id="EFT" style="display:none;">EFT</div>
0
Comment
Question by:mariita
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:nschafer
ID: 16873041
Hi mariita,

Try this.

function showDiv(divID) {
  document.getElementById('EFT').style.display='none';
  document.getElementById('credit').style.display='none';
  document.getElementById('debit').style.display='none';
  document.getElementById(divID).style.display = 'block';
}

Hope this helps,
Neal.
0
 

Author Comment

by:mariita
ID: 16873081
Is there a way to do it generically?
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 16873101
function showDiv(divID) {
      var divs = ("credit,debit,eft").split(',');
      for (ix=0; ix < divs.length; ix++) {
            if (divs[ix].match(divID)) {
                 document.getElementById(divs[ix]).style.display = 'block';
            }
            else {
                 document.getElementById(divs[ix]).style.display = 'none';
            }
      }
}


<select name="paymentAccountType" onchange="showDiv(this.value);" >
     <option value="">Select Option</option>
     <option value="credit">Credit card</option>
     <option value="debit">Debit card</option>
     <option value="eft">EFT</option>
</select>
<div id="credit" style="display:none;">Credit card</div>
<div id="debit" style="display:none;">Debit card</div>
<div id="eft" style="display:none;">EFT</div>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Accepted Solution

by:
Pravin Asar earned 250 total points
ID: 16873129
Following is more generic, which passes the str (delimited div ids)

function showDiv(divID, divstr) {
      var divs = divstr.split(',');
      for (ix=0; ix < divs.length; ix++) {
            if (divs[ix].match(divID)) {
                 document.getElementById(divs[ix]).style.display = 'block';
            }
            else {
                 document.getElementById(divs[ix]).style.display = 'none';
            }
      }
}

<select name="paymentAccountType" onchange="showDiv(this.value, 'credit,debit,eft');" >
     <option value="">Select Option</option>
     <option value="credit">Credit card</option>
     <option value="debit">Debit card</option>
     <option value="eft">EFT</option>
</select>
<div id="credit" style="display:none;">Credit card</div>
<div id="debit" style="display:none;">Debit card</div>
<div id="eft" style="display:none;">EFT</div>

0
 

Author Comment

by:mariita
ID: 16873134
But is there a way to hide the non-selected DIVS while not actually naming them in a variable? Otherwise I would have to use a separate Javascript function for every SELECT.
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 16873380
you can find all div in current page . but how to relate them with select .
Hence

last post, gives flexibility to supply the div ids , by that way you could use same function for different
select .


0
 

Author Comment

by:mariita
ID: 16873398
Yes, I actually sent my last comment before seeing your solution.

Thanks again for your help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery Parallax 1 38
regx  exclude  pattern 6 29
Angular.js example vocabulary extensions 5 35
insert text field data into html script 13 30
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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 …
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…

864 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

24 Experts available now in Live!

Get 1:1 Help Now