Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Toggle display of DIV based on onSelect event handler

Posted on 2006-06-09
7
Medium Priority
?
604 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 29

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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 29

Accepted Solution

by:
Pravin Asar earned 1000 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 29

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
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…

877 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