By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

Get a list of elements inside a tag

P: n/a
How can I get a list of elements inside a tag and when I find one
element of a type that I choose I can change its ID and CLASS
attribute?

Ex:
<div id="content"> <!-- I want to get the list of elements inside the
DIV with ID content -->
<a href="#">My link</a>
<b>Some trash</b>
<div> <!-- The first DIV element I find, will be used and I'll change
it's ID -->
<div id="innerdiv1">
</div>
<div id="innerdiv2">
</div>
</div>
</div>

How can I do this?

Jul 1 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Juliano.net" <ju*********@gmail.comwrites:
How can I get a list of elements inside a tag and when I find one
element of a type that I choose I can change its ID and CLASS
attribute?
var contentDiv = document.getElementById("content");

// do something to first div element:
var containedDivElements = contentDiv.getElementsByTagName("div");
var firstDiv = containedDivElements[0];

firstDiv.id = "changedId";
firstDiv.className = "changedClass";

// run through all contained elements:
var allContainedElements = contentDiv.getElementsByTagName("*");
for (var i = 0; i < allContainedElements.length; i++) {
var elem = allContainedElements[i];
// do something with contained elem
}
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 1 '06 #2

P: n/a
Lasse, how can I get the name and values for all attributes of a tag?

And is there any JavaScript variables watcher (debugger) that allows
seeing the variables values changing? Like a Delphi or Visual Basic
debugger.

Lasse Reichstein Nielsen wrote:
"Juliano.net" <ju*********@gmail.comwrites:
How can I get a list of elements inside a tag and when I find one
element of a type that I choose I can change its ID and CLASS
attribute?

var contentDiv = document.getElementById("content");

// do something to first div element:
var containedDivElements = contentDiv.getElementsByTagName("div");
var firstDiv = containedDivElements[0];

firstDiv.id = "changedId";
firstDiv.className = "changedClass";

// run through all contained elements:
var allContainedElements = contentDiv.getElementsByTagName("*");
for (var i = 0; i < allContainedElements.length; i++) {
var elem = allContainedElements[i];
// do something with contained elem
}
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.