Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get all child elements within a DIV?

Status
Not open for further replies.

ericse

Programmer
Apr 19, 2007
32
US
Hey guys-

I can't seem to do this. Everything I try fails.

I have the ID of the Div... and I create controls dynamically within that div. I want to change the styles of those elements within the div.

How can I do this?

I've tried: var children = document.getElementById('id').getChildren();

but it fails saying getChildren() is not a function.

.childNodes also fails. I just don't get it.

Please help?

THanks
 
Hi

[tt]childNodes[/tt] must work.
Code:
var children = document.getElementById('id').childNodes;

[gray]// or[/gray]

var children = document.getElementById('id').getElementsByTagName('*');
Note that there is a difference between the above two : the first returns the list of direct descendants, while the second returns a list of all descendants.

Feherke.
 
I think you need to revisit this:
.childNodes also fails. I just don't get it.

Give your div a different id other than 'id'

Code:
var childNodeArray = document.getElementById('somethingOtherThanid').childNodes;

If that doesn't work, then your problem lies somewhere else.

[monkey][snake] <.
 
Yeah, I really didn't think using 'id' will cause it to error, however, it causes bad practice and possible interference later on.

[monkey][snake] <.
 
Hey guys, I wasn't actually using 'id' heh, I just used that as an example because I was lazy and didn't want to type out the *real* id.. Anyway, I got it to work using getElementsByTagName("*") so thanks for the help :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top