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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.