Forum Moderators: open
Example
<style>
.show{
display = '';
}
</style>
<a href="#" onlick="changeStyle()">Change</a>
<div id="data_div" class="show">layer 1</div>
<div id="data_div" class="show">layer 1</div>
<div id="data_div" class="show">layer 1</div>
<div id="data_div_sub" class="show">layer 1</div>
<div id="data_div" class="show">layer 1</div>
<div id="data_div_sub" class="show">layer 1</div>
<div id="data_div_sub" class="show">layer 1</div>
Would like to hide all the layers with the id name = "data_div". Izzit possible to do this without using a unique id name for each div?
var divs = document.getElementsByTagNames('div');
for(var i = 0; i < divs.length; i++)
{
if(divs[i].id.substr(0, 8) == 'data_div')
{
divs[i].style.visibility = 'hidden';
}
}
just give the divs you want to use ids that start with data_div, like data_div1, data_div2, data_div3 and those, you don't want to hide, ids that don't start with data_div, like, subdata_div1, subdata_div2.
haven't tested it, but id should work. more or less.
Would like to hide all the layers with the id name = "data_div". Izzit possible to do this without using a unique id name for each div?
Also, your style rule is wrong. You don't use = in stylesheets. So instead of display = '' it should be display: auto;
Example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<!-- Styles -->
<style type="text/css">
.show {
display: auto;
}
.hide {
display: none;
}
</style>
<title>Untitled</title>
</head>
<body>
<a href="#" onclick="changeStyle()">Change</a>
<div class="show data_div">layer 1</div>
<div class="show data_div">layer 1</div>
<div class="show data_div">layer 1</div>
<div class="show data_div_sub">layer 1</div>
<div class="show data_div">layer 1</div>
<div class="show data_div_sub">layer 1</div>
<div class="show data_div_sub">layer 1</div>
<!-- Scripts -->
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/yahoo/yahoo-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.5.2/build/dom/dom-min.js"></script>
<script type="text/javascript">
function changeStyle() {
var D = YAHOO.util.Dom;
D.setStyle(D.getElementsByClassName('data_div', 'div'), 'display', 'none');
// OR
// D.replaceClass(D.getElementsByClassName('data_div', 'div'), 'show', 'hide');
}
</script>
</body>
</html>