i didn't write this script completely myself...i'm not much of a javascript person, so i pieced it together (it's a pretty common function, i think)...anyway, you should be able to just copy and paste all of the code into a new file and it should run just fine without errorsthere are two separate things i want to do with it:1.) when one of the drop-down contents are open, and you click another, it hides the first one again2.) i'd like to have it where there is a single drop-down box (as in a form), and when you make a choice, it displays the content (so that all content is loaded, no need for refreshing, and it dynamically changes the display based on the drop box selection)also, if you have any suggestions for cleaning this up (i can muddle through, but really only done basic javascripts) or simplifying the code, they're appreciated
<!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>javascript drop content</title><style media="screen" type="text/css">div, p { font-family: Verdana, Geneva, sans-serif; font-size: 0.7em; margin: 0; padding: 0;}ul, li { padding: 0;}ul { margin: 0 0 0 10px;}li { margin: 0 0 0 5px;}.contentBox { background-color: #ccc; border: 1px solid black; height: 0px; margin: 5px 0 0 10px; overflow: hidden; position: relative; /* change to absolute to display on top of other content */ visibility: hidden; width: 350px;}.content { font-family: "Trebuchet MS", Arial, Helvetica, sans-serif; padding: 1px 7px 5px; position: relative; width: 100%;}</style><script language="javascript" type="text/javascript">var slideDownInitHeight = new Array();var slidedown_direction = new Array();var slidedownActive = false;var contentHeight = false;var slidedownSpeed = 5; // higher = fastervar slidedownTimer = 1; // lower = fasterfunction slidedown_showHide(boxId) { if(!slidedown_direction[boxId])slidedown_direction[boxId] = 1; if(!slideDownInitHeight[boxId])slideDownInitHeight[boxId] = 0; if(slideDownInitHeight[boxId] == 0) { slidedown_direction[boxId] = slidedownSpeed; } else { slidedown_direction[boxId] = slidedownSpeed*-1; } slidedownContentBox = document.getElementById(boxId); var subDivs = slidedownContentBox.getElementsByTagName('div'); for(var no=0; no<subDivs.length; no++) { if(subDivs[no].className=='content')slidedownContent = subDivs[no]; } contentHeight = slidedownContent.offsetHeight; slidedownContentBox.style.visibility = 'visible'; slidedownActive = true; slidedown_showHide_start(slidedownContentBox,slidedownContent);}function slidedown_showHide_start(slidedownContentBox,slidedownContent) { if(!slidedownActive) { return; } slideDownInitHeight[slidedownContentBox.id] = slideDownInitHeight[slidedownContentBox.id]/1 + slidedown_direction[slidedownContentBox.id]; if(slideDownInitHeight[slidedownContentBox.id] <= 0) { slidedownActive = false; slidedownContentBox.style.visibility = 'hidden'; slideDownInitHeight[slidedownContentBox.id] = 0; } if(slideDownInitHeight[slidedownContentBox.id]>contentHeight) { slidedownActive = false; } slidedownContentBox.style.height = slideDownInitHeight[slidedownContentBox.id] + 'px'; slidedownContent.style.top = slideDownInitHeight[slidedownContentBox.id] - contentHeight + 'px'; setTimeout('slidedown_showHide_start(document.getElementById("' + slidedownContentBox.id + '"),document.getElementById("' + slidedownContent.id + '"))',slidedownTimer);}</script></head><body><ul> <li><a href="#" onclick="slidedown_showHide('box1');return false;">content 1</a> <div class="contentBox" id="box1"><div class="content" id="subBox1"><!-- slide down content goes here --> <p>Ma quande lingues coalesce, li grammatica del resultant lingue es plu simplic e regulari quam ti del coalescent lingues.</p> <!-- End slide down content --></div></div></li> <li>other content</li> <li><a href="#" onclick="slidedown_showHide('box2');return false;">content 2</a> <div class="contentBox" id="box2"><div class="content" id="subBox2"><!-- slide down content goes here --> <p>Ma quande lingues coalesce, li grammatica del resultant lingue es plu simplic e regulari quam ti del coalescent lingues.</p> <!-- End slide down content --></div></div></li></ul></body></html>
1/7/2009 11:39:58 AM
Just create a dropdown object so you can keep tabs on which is open and which needs to be closed when you click on a new dropdown. Example:var dropdown = new function(){ this.currentId = ""; this.reset = function() { this.currentId = ""; }}then in your javascript for opening and closing dropdowns you can run a check.if (dropdown.currentId == ""){ //open the clicked dropdown since no other is currently open}else if (dropdown.currentId == id){ //close the dropdown}else if (dropdown.currentId != id){ // close the dropdown with id of dropdown.currentId // open the newly clicked dropdown}oh forgot to add:when you open a dropdown you would calldropdown.currentId = id;or when you close a dropdown and don't open a new one you would calldropdown.reset;[Edited on January 7, 2009 at 11:54 AM. Reason : .]
1/7/2009 11:53:47 AM
use jquery insteadhttp://ui.jquery.com/demos/accordion
1/7/2009 12:00:22 PM
^i'll never go back to not using a JS-framework except for only the tiniest things
1/7/2009 2:30:31 PM
i like prototype and scriptacuolus
1/7/2009 2:50:40 PM
also good choices
1/7/2009 3:03:35 PM
i, too, prefer jquerybut this needs to be a single file, and it seems like overkill to include all of the extra jquery behavior code in the file for this one simple task, especially when it's almost completely put together...yes?
1/7/2009 3:05:33 PM
I gotta be honest here, I'm too lazy to interpret that code or paste it into a file, and otherwise can't tell specifically what you're askingput the HTML up somewhere so people can see what you mean and I am guessing the discussion will be less about how prototype is awesome and more about your specific problem incidentally, prototype is pretty awesome
1/7/2009 5:25:13 PM
okay, another (simpler) question...i am not at all a javascript person so i've got a form with check boxes...when you select a check box, i want to show previously hidden data...this is easy and there are dozens of ways to do it...i went with this (css, script, implementation):
.hide{display:none;visibility:hidden;}.show{display:block;visibility:visible;}function toggle(id){ var e = document.getElementById(id); if(e.className == "show") e.className = "hide"; else e.className = "show";}<input type="checkbox" name="option_other" onclick="toggle('other_show');"/>Other (explain below)<br /><div id="other_show" class="hide"><textarea name="option_other" rows="2" cols="25" wrap="virtual"></textarea></div>
window.onload = function uncheck() { var tag = document.getElementsByName('input'); for(var i=0; i<tag.length; i++) { if(tag[i].getAttribute('type') == 'checkbox') { tag[i].checked = false; } }}
3/13/2009 11:34:57 AM
3/13/2009 11:45:25 AM
3/13/2009 2:24:43 PM
^or cmd+shift+R on a Mac
3/13/2009 2:25:53 PM
SONOFABITCH >.<thanks, y'all
3/13/2009 2:27:36 PM