visibility - How do I get div not to display if value in another div is empty in Javascript? -
here code. have tried can think of. have tried using div id's , have tried classes. nothing seems work. want number 2 not visible if there no entry beside it. doesn't matter if in table or not.
thanks.
<style type="text/css"> <!-- .leftone { float: left; height: auto; width: auto; } .rightone { float: left; height: auto; width: auto; } .lefttwo { float: left; height: auto; width: auto; } .righttwo { float: left; height: auto; width: auto; } --> </style> <table width="400" border="0" cellpadding="0" cellspacing="3" id="tableone"> <tr> <td width="200" height="50"><div class="leftone">1.)</div></td> <td width="200" height="50"><div class="rightone">the number one</div></td> </tr> <tr> <td width="200" height="50"><div class="lefttwo">2.)</div></td> <td width="200" height="50"><div class="righttwo"></div></td> </tr> </table> <p> </p> <script type="text/javascript"> <!-- function shownumbers() { var mynum1 = '[.rightone]'; if(mynum1 != ''){ document.getelementbyid('.leftone').style.display = "block"; } else if(mynum1 == ''){ document.getelementbyid('.leftone').style.display = "none"; } var mynum2 = '[.righttwo]'; if(mynum2 != ''){ document.getelementbyid('.lefttwo').style.display = "block"; } else if(mynum2 == ''){ document.getelementbyid('.lefttwo').style.display = "none"; } } //--> </script>
you cannot use getelementbyid classes. also, don't need '.' or '#' when using these methods in javascript. below should asking. although if there ever 1 item of class 'rightone' , 'leftone' should use id's.
var mynum1 = document.getelementsbyclassname('rightone')[0].innerhtml; if(mynum1 != ''){ document.getelementsbyclassname('leftone')[0].style.display = 'block'; } else if(mynum1 == ''){ document.getelementsbyclassname('leftone')[0].style.display = 'none'; }
a more elegant solution be:
html:
<table width="400" border="0" cellpadding="0" cellspacing="3" id="tableone"> <tr> <td><div class="left">1.)</div></td> <td><div class="right">the number one</div></td> </tr> <tr> <td><div class="left">2.)</div></td> <td><div class="right"></div></td> </tr> </table>
js:
var right = document.getelementsbyclassname('right'); for(var i=0;i<right.length;i++){ if(!right[i].innerhtml){ right[i].parentnode.parentnode.getelementsbyclassname('left')[0].style.display = 'none'; } else { right[i].parentnode.parentnode.getelementsbyclassname('left')[0].style.display = 'right'; } }
Comments
Post a Comment