php - javascript to change style display -
i have bunch of items setup <div id="first" class="source">
i'm in google chrome, , when page loads hides items when click onclick button un hide them, i'm not able click again , make hide.
html
<body onload="setup();"> <a href="#first" onclick="shoh('first');" >
js:
function setup() { foo = document.getelementbyid('first'); foo = document.getelementsbyclassname('source'); (c=0; c<foo.length; c++) { foo[c].style.display='none' } } function shoh(id) { if (document.getelementbyid(id).style.display = 'none'){ var divs = document.getelementbyid(id); for(var i=0; i<divs.length; i++) { divs[i].style.display='block' } } else { cow = document.getelementbyid(id); for(a=0; a<cow.length; a++) { cow[a].style.display='none' } } }
you have couple of issue here:
if compare 2 variables need use ==
, therefore should be:
document.getelementbyid(id).style.display == 'none'
also, using getelementbyid
, returns single node, not array. therefore should not loop on returned value:
var div = document.getelementbyid(id); div.style.display='block';
so in end, should this:
function shoh(id) { if (document.getelementbyid(id).style.display == 'none'){ var div = document.getelementbyid(id); div.style.display='block'; } else { cow = document.getelementbyid(id); cow.style.display='none'; } }
Comments
Post a Comment