Solved

Toggle display of DIV based on onSelect event handler

Posted on 2006-06-09
7
596 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
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.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

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

12 Experts available now in Live!

Get 1:1 Help Now