Jump to content

Can't get firstChild of a div


The Little Guy

Recommended Posts

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);
}

Link to comment
https://forums.phpfreaks.com/topic/218042-cant-get-firstchild-of-a-div/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.