Creating function to expand/close all nodes
Hey guys,
I got these codes:
JS:
Code:
<script language="JavaScript" type="text/javascript">
function openClose(theID) {
if (document.getElementById(theID).style.display == "block") { document.getElementById(theID).style.display = "none" }
else { document.getElementById(theID).style.display = "block" } }
</script>
CSS:
Code:
.texter {display:none} @media print {.texter {display:block;}}
HTML:
Code:
<a class= "link_item" href="#" onclick="openClose('a2'); return false;">Link</a><br/>
<br/><br/>
<div id="a2" class="texter">BLA BLA BLA</div>
I want to create an "Open/Close all" function.
All my tries have not succeeded.
Please help me.
Regards!
Code:
<!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" xml:lang="en" lang="en">
<head>
<title></title>
<style type="text/css">
/*<![CDATA[*/
.texter {display:none} @media print {.texter {display:block;}}
/*]]>*/
</style>
<script type="text/javascript">
/*<![CDATA[*/
function Open(cls,bid,open,close){
var b=document.getElementById(bid),reg=new RegExp('\\W'+cls+'\\W'),els=document.getElementsByTagName('DIV'),z0=0;
b.value=b.value==open?close:open;
for (;z0<els.length;z0++){
if(reg.test(' '+els[z0].className+' ')){
els[z0].style.display=b.value==close?'block':'none';
}
}
}
/*]]>*/
</script>
</head>
<body>
<input id="b" type="button" name="" value="Open" onmouseup="Open('texter','b','Open','Close');"/>
<div id="a2" class="texter">BLA BLA BLA</div>
<div id="a3" class="texter">BLA BLA BLA</div>
</body>
</html>
1. Thank you for helping.
2. Sorry,
But I think you got me wrong.
My goal is to add a function (to the current expand/close specific).
Or I got you wrong... - You did not post HTML code neither.
Regards.
jquery
In jquery, there's a function called toggle() that will help.
<script language="JavaScript" type="text/javascript">
function openCloseAll(){
$('a').toggle();
}
</script>
of course, this supposes that all tags are visible or all tags are closed.
If one must remain open or closed when you call the function, change it in this way:
<script language="JavaScript" type="text/javascript">
function openCloseAll(){
$('a').toggle();
$(this).toggle();
}
</script>
<a href="#" onclick="openCloseAll()">abc</a>
adding a class let's you filter the target anchors...
<script language="JavaScript" type="text/javascript">
function openCloseAll(){
$('a.openclose').toggle();
$(this).toggle();
}
</script>
<a href="#" onclick="openCloseAll()" class="openclose">abc</a>
Hey,
I've tried the function but it hides most parts of my webpage
It's only a question of choosing the right selector.
Adding classes helps to filter your selection.
http://api.jquery.com/category/selectors/
methods hide(), show(), toggle(), fadeIn(), fadeOut(), addClass(), removeClass() and css() will be the most usefull to play on the visibility of your elements.
http://api.jquery.com/category/css/
http://api.jquery.com/category/effects/
I hope this will be of some help
Last edited by Gudlife; 09-16-2012 at 04:39 AM .
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks