Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Dynamically Load Content into DIV From <SELECT>
Old 06-19-2007, 12:58 PM Dynamically Load Content into DIV From <SELECT>
Average Talker

Posts: 23
Trades: 0
As the title says, I am looking for a script that will hide/unhide the content of a <div> based on the selection from a dropdown box. I have a script that displays text strings inside of a designated div, but that uses text strings and I need to add lots of code, so I need the ability to place my code blocks within a div that is simply unhidden when the correct option is selected. Thanks!
RRasco is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-19-2007, 02:41 PM Re: Dynamically Load Content into DIV From <SELECT>
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Simple...

HTML Code:
<script type='text/javascript'>
function getStyle( element, cssRule ){
  if( document.defaultView && document.defaultView.getComputedStyle ){
    var value = document.defaultView.getComputedStyle( element, '' ).getPropertyValue( 
      cssRule.replace( /[A-Z]/g, function( match, char ) { 
        return "-" + char.toLowerCase(); 
      } ) 
    );
  }
  else if ( element.currentStyle ) {
    var value = element.currentStyle[ cssRule ];
  }
  else{
    var value = false;
  }
  return value;
}

function toggle(_id){
  //We reset every divs in a hidden state
  var aryDivs=document.getElementsByTagName('div');
  var cpt=0;
  for(cpt=0;cpt<aryDivs.length;cpt++){
    //we check for IE and FF
    if(aryDivs[cpt].getAttribute('class')=='hidden' || aryDivs[cpt].className=='hidden'){
      aryDivs[cpt].style.display='none';
    }
  }
  
  //And we only show 1 precise div
  var _trg=document.getElementById('sel'+_id);
  if(!_trg){
    return false;
  }
  else{
    if(getStyle(_trg,'display')=='block'){
      _trg.style.display='none';
    }
    else{
      _trg.style.display='block';
    }
  }
}
</script>
<style type='text/css'>
.hidden{
  display:none;
}
</style>
<div id='sel1' class='hidden'>
  blah blah blah
</div>
<div id='sel2' class='hidden'>
  blih blih blih
</div>
<div id='sel3' class='hidden'>
  bluh bluh bluh
</div>
<div id='sel4' class='hidden'>
  bloh bloh bloh
</div>
<select id='sel1' onchange="javascript:toggle(this.options[this.selectedIndex].value);">
  <option value="1">Show div 1</option>
  <option value="2">Show div 2</option>
  <option value="3">Show div 3</option>
  <option value="4">Show div 4</option>
</select>
Ask me if you have question about this script.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 06-19-2007 at 03:03 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-19-2007, 04:33 PM Re: Dynamically Load Content into DIV From <SELECT>
Average Talker

Posts: 23
Trades: 0
Tripy, you must be some sort of angel watching over me, every time I post, you respond. I actually got my current JS div stuff modified to work with my selects, but once again, THANK YOU!
RRasco is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Dynamically Load Content into DIV From <SELECT>
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11947 seconds with 12 queries