The Little Guy Posted November 7, 2010 Share Posted November 7, 2010 When I do this: var netFeed = document.getElementById('netFeed'); var fn = netFeed.firstChild; alert(fn); my alert says [object text] Shouldn't it say [object div]? Basically I want to get the background color of the first div in netFeed, in this case it is _20 Later in my javascript I have fn.style.backgroundColor and I get this error message: Uncaught TypeError: Cannot read property 'backgroundColor' of undefined <div id="netFeed"> <div id="_20" class="networkFeed" style="background-color:#e7efff;"> <div class="commentImage"> <img src="http://img.publicsize.com/profileImages/50//.jpg"> </div> <div class="commentComment"> <b><span class="link" onclick="loadBody('profile', 'id', '1')">You</span> said:</b><br> <p>asdf sfd sfd fd sf </p> <p>From the <span class="bold">PHP</span> network <span style="color: #999999;">Sunday November 07<sup>th</sup>, 2010</span></p> </div> </div> </div> How can I fix this problem? Full JS if you need it: function postNetworkComment(id){ var connect = Post(); var message = document.getElementById('networkCommentBox').value; connect[0].onreadystatechange = function(){ if(connect[0].readyState == 4){ var netFeed = document.getElementById('netFeed'); var fn = netFeed.firstChild; alert(fn); var opt = document.createElement('div'); opt.innerHTML = connect[0].responseText; opt.className = 'networkFeed'; var bgc; if(fn.style.backgroundColor == 'rgb(231, 239, 255)'){ bgc = '#ffffff'; }else{ bgc = '#e7efff'; } netFeed.insertBefore(opt, netFeed.firstChild); netFeed.firstChild.style.backgroundColor = bgc; /*document.getElementById('postCBlock').innerHTML = message;*/ } }; var va = 'message='+escape(message); connect[0].open("POST", '/?p=postNetworkComment&id='+escape(id), true); connect[0].setRequestHeader("Content-Type", connect[1]); connect[0].send(va); } Quote Link to comment https://forums.phpfreaks.com/topic/218042-cant-get-firstchild-of-a-div/ Share on other sites More sharing options...
The Little Guy Posted November 7, 2010 Author Share Posted November 7, 2010 problem solved. It was because I had some white space. Quote Link to comment https://forums.phpfreaks.com/topic/218042-cant-get-firstchild-of-a-div/#findComment-1131511 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.