I have some divs on my page that when a link is clicked they need to toggle (show/hide)
JS
var state = 'none';
function showhide(layer_ref) {
//alert(eval( "document.all." + layer_ref + ".style.display") == "none")
if (document.all) { //IS IE 4 or 5 (or 6 beta)
if(eval( "document.all." + layer_ref + ".style.display") == "none"){
eval( "document.all." + layer_ref + ".style.display = 'block'");
}else{
eval( "document.all." + layer_ref + ".style.display = 'none'");
}
}
if (document.layers) { //IS NETSCAPE 4 or below
if(document.layers[layer_ref].display == none){
document.layers[layer_ref].display = "block";
}else{
document.layers[layer_ref].display = "none";
}
}
if (document.getElementById &&!document.all) {
if(document.getElementById(layer_ref).style.display == "none"){
document.getElementById(layer_ref).style.display = "block";
}else{
document.getElementById(layer_ref).style.display = "none";
}
}
}
HTML
<div class="faq-row" style="z-index: 976;">
<span class="itp-title"><a href="#" onclick="showhide('div1');">title</a></span>
<div id="div1" style="display: none;">divcont</div>
</div>
<div class="faq-row" style="z-index: 976;">
<span class="itp-title"><a href="#" onclick="showhide('div1');">title</a></span>
<div id="div2" style="display: none;">divcont</div>
</div>
<div class="faq-row" style="z-index: 976;">
<span class="itp-title"><a href="#" onclick="showhide('div1');">title</a></span>
<div id="div3" style="display: none;">divcont</div>
</div>
my problem is that no matter what link i click it only toggles the first div? Cany anybody see where im going wrong?
Thanks
Your first issue is trying to write non-trivial crossbrowser JavaScript. This would be much, much simpler if you used something like jQuery.
That said, your issue is that your
onClickhandlers are all pointing to the sameid.Change these to the relevant div id’s and you should be fine.
If you used something like jQuery, this function would be irrelevant though and you could just do something like: