Get a list of elements inside a tag

J

Juliano.net

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?
 
L

Lasse Reichstein Nielsen

Juliano.net said:
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;
// do something with contained elem
}


/L
 
J

Juliano.net

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.
Juliano.net said:
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;
// do something with contained elem
}


/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top