i have issue javascript. want add innerhtml div if empty. code, please me review. div same class, not use id , using javascript. html:
<div class="b">test</div> <br/> <div class="b"></div> <br/>
javascript:
function test(){ var x = document.getelementsbyclassname("b"); (i = 0; < x.length; i++) { if (x[i].length() < 0){ x[i].innerhtml = "add test"; } } }
you need use x[i].innerhtml.length
instead of x[i].length()
, wont less zero @ minimum zero.
few of corrections made in code
- took length of innerhtml instead of html element div
- length property parenthesis should not used
- length wont less 0 condition should check if length zero.
- for being on safe side trim innerhtml before take length.
function test(){ var x = document.getelementsbyclassname("b"); (i = 0; < x.length; i++) { if (x[i].innerhtml.trim().length == 0){ x[i].innerhtml = "add test"; } } }
Comments
Post a Comment