473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Get a list of elements inside a tag

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
2 26978
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Julia Briggs | last post by:
Hello, is there anyway to insert an image on any particular <select> option line in a drop down list without using a specialty drop-down? Any good ones out there that can be made to resemble a...
3
by: abro | last post by:
Problem: A list contained in a div contains several items that are made of two parts: itemName and itemValue. ie: <div id="data"> <li>longtime1 <span> 1326 mins></span></li> <li>longtime2...
3
by: yawnmoth | last post by:
I'm trying to center list elements in a webpage I'm working on, and setting margin-left to auto for ol (or ul) seems to prevent the number (or bullet) from displaying in IE6 (strict mode) and...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
1
by: platostoteles | last post by:
Hallo NG, I am new to JavaScript and would really appreciate any help to solve my problem. I am using the blow code in my form to validate form fields. What I would like to accomplish is that...
5
by: shapper | last post by:
Hello, I have the following list: <ul id="parent" class="parent"> <li> <img... </li> <ul id="child" class="child"> <li>Message 1</li>
13
by: shapper | last post by:
Hello, I have the following code: <div id="outer" class="outer"> <ol> <li>Item outer 01</li> <li>Item outer 02</li> </ol> <div id="inner" class="inner">
11
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter'...
4
by: Keith Hughitt | last post by:
For example, If you have a list: <ul> <li>item 1 is short.</li> <li>item 2 is a little bit longer</li> </ul> regardless of the size of the contents of each list item, the element
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.