473,406 Members | 2,698 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.

Grab elements of DL ( definition List )

Hi Everyone,

I'm trying to grab all of the elements of a DL, specifically the <a href>'s
grouping them by the DD's. I suppose if I can just get them into groups I
can get the href's later. The hard part is getting them grouped as explained
below.

For example...

<dl id="dlList">
<dt><a href="#2">DT Item1<span>(1)</span></a></dt>
<dd><a href="#">DD Item1<span>(2)</span></a></dd>
<dd><a href="#">DD Item2<span>(1)</span></a></dd>
<dd><a href="#">DD Item3<span>(1)</span></a></dd>

<dt><a href="#1">DT Item1<span>(1)</span></a></dt>
<dd><a href="#">DD Item1<span>(1)</span></a></dd>
<dd><a href="#">DD Item2<span>(1)</span></a></dd>
</dl>

Is there a way to say, loop through the DL until it finds a DT. Whe it finds
one, grab it and all of the DD's that immediately follow it .. until it
comes to another DT. Group it with its DD's and continue until no more DT's
are found.

Then maybe take these collections and possibly populate an array with the
groups?

I really don't know how to go about this.

David J.


Aug 30 '06 #1
11 2350
Using DOM:

var root = document.getElementById("dlList"); // the parent...

var dts = root.getElementsByTagName("dt"); // the dt's in the list...

for (var i = 0; i < dts.length; i++) {
var dds = dts.getElementsByTagName("dd"); // the dd's of the dt...
for (var j = 9; j < dds.length; j++) {
var dd = dds[j];
//do stuff with your dd...
}
}

//the end.

Aug 30 '06 #2
Using DOM:

var root = document.getElementById("dlList"); // the parent...

var dts = root.getElementsByTagName("dt"); // the dt's in the list...

for (var i = 0; i < dts.length; i++) {
var dds = dts.getElementsByTagName("dd"); // the dd's of the dt...
for (var j = 0; j < dds.length; j++) {
var dd = dds[j];
//do stuff with your dd...
}
}

//the end.

Aug 30 '06 #3
Using DOM:

var root = document.getElementById("dlList"); // the parent...

var dts = root.getElementsByTagName("dt"); // the dt's in the list...

for (var i = 0; i < dts.length; i++) {
var dds = dts[i].getElementsByTagName("dd"); // the dd's of each
dt...
for (var j = 9; j < dds.length; j++) {
var dd = dds[j];
//do stuff with your dd...
//for example...var anchor = dd.getElementsByTagName("a");
// var href = anchor.href;
}
}

//the end.

Aug 30 '06 #4
Using DOM:

var root = document.getElementById("dlList"); // the parent...

var dts = root.getElementsByTagName("dt"); // the dt's in the list...

for (var i = 0; i < dts.length; i++) {
var dds = dts[i].getElementsByTagName("dd"); // the dd's of each
dt...
for (var j = 0; j < dds.length; j++) {
var dd = dds[j];
//do stuff with your dd...
//for example...var anchor = dd.getElementsByTagName("a");
// var href = anchor.href;
}
}

//the end.

Aug 30 '06 #5
"Tom Cole" wrote in message
Using DOM:

var root = document.getElementById("dlList"); // the parent...

var dts = root.getElementsByTagName("dt"); // the dt's in the list...

for (var i = 0; i < dts.length; i++) {
var dds = dts.getElementsByTagName("dd"); // the dd's of the dt...
for (var j = 9; j < dds.length; j++) {
var dd = dds[j];
//do stuff with your dd...
}
}

//the end.
I don't think that will work, because the DDs aren't nested inside of the
DLs like most HTML tags are, so the length comes up 0 for each one.

David J.
Aug 30 '06 #6
It's as if you need to loop through the DL until you find a DT. When you do,
grab it and all of the following DDs until the next DT is found. When the
nex DT is found, put all that was just found into a collection of sorts ( an
array entry possibly for use later ) and continue onto the next DT and its
DDs until it comes upon another DT ( until it doesn't ).

David J.
Aug 30 '06 #7
Here is what I have so far. I can get all of the elements inside of the DL
into an array, how to separate them from here into the groups descibed
before is the pickle.

http://mysite.verizon.net/microweb2/list.html

David J.
Aug 30 '06 #8
David wrote:
Here is what I have so far. I can get all of the elements inside of the DL
into an array, how to separate them from here into the groups descibed
before is the pickle.
Get the DL element, traverse the child nodes, store an object for each
new DT that is encountered. The first property is a reference to the
DT, the second property is an array of the sibling DDs until the next
DT.

The following example looks much more comlpex than I think it needs to
be, I'm sure there's a simpler way...

<title>DL Play</title>
<script type="text/javascript">

function makeDTGroups(id){
var dl = document.getElementById(id);
var dtGroup, dtGroups = [];
var dtNum = 0;
var ddNum = 0;
var i = 0;
var node;
var currentDDGroup;
while ( (node = dl.childNodes[i++]) ){
if (1 == node.nodeType){
if ('dt' == node.nodeName.toLowerCase()){
dtGroups[dtNum] = {};
dtGroup = dtGroups[dtNum++];
dtGroup['dt'] = node;
dtGroup['dd'] = [];
currentDDGroup = dtGroup['dd'];
ddNum = 0;
} else if (currentDDGroup &&
'dd' == node.nodeName.toLowerCase()){
currentDDGroup.push(node);
}
}
}

/* dTgroups is an Array of objects of form:
[
{dt : <ref to first dt>,
dd : [ddRef0, ddRef1, ddRef2, ...]
},
{dt : <ref to 2nd dt>,
dd : [ddRef0, ddRef1, ddRef2, ...]
},
...
];
*/

// Now do something with it.
alert( showObj(dtGroups).join('\n'));

// Show the innerHTML of the 2nd DD of the 3rd DT
// remember that they are zero indexed:
alert( dtGroups[2].dd[1].innerHTML );

// Get a reference to the 1st dt element:
alert( dtGroups[0].dt.nodeName );
}

// Does something with the object
function showObj(obj, msg, pad){
var t, nName;
msg = (msg)? msg : [];
pad = ('string' == typeof pad)? pad + '_' : '';

for (var p in obj){
t = pad + p;
if (obj[p].nodeName){
msg.push(t + ' : ref to ' + obj[p].nodeName);
} else {
if (obj[p].constructor.name) {
msg.push(t + ': ' + obj[p].constructor.name);
} else {
msg.push(t + ': ' + typeof obj[p]);
}
showObj(obj[p], msg, pad);
}
}
return msg;
}

</script>

<dl id="dlList">
<dt><a href="#">dt0</a></dt>
<dd><a href="#">dt0dd0</a></dd>
<dd><a href="#">dt0dd1</a></dd>
<dd><a href="#">dt0dd2</a></dd>

<dt><a href="#1">dt1</a></dt>
<dd><a href="#">dt1dd0</a></dd>
<dd><a href="#">dt1dd1</a></dd>

<dt><a href="#1">dt2</a></dt>
<dd><a href="#">dt2dd0</a></dd>
<dd><a href="#">dt2dd1</a></dd>
<dd><a href="#">dt2dd2</a></dd>
<dd><a href="#">dt2dd3</a></dd>
</dl>

<button onclick="makeDTGroups('dlList');">makeDDGroups()</button>

Aug 30 '06 #9

RobG wrote:
[...]
The following example looks much more comlpex than I think it needs to
be, I'm sure there's a simpler way...
A bit more concise:

function makeDTGroup(id){
var node = document.getElementById(id).firstChild;
var nodeName;
var dts = [];
var dtNum = 0;
var ddGroup;
do {
if (1 == node.nodeType){
nodeName = node.nodeName.toLowerCase();
if ('dt' == nodeName){
dts.push({dt:node, dd: []});
ddGroup = dts[dtNum++].dd;
} else if (ddGroup && 'dd' == nodeName){
ddGroup.push(node);
}
}
} while( (node = node.nextSibling) )
return dts;
}
--
Rob

Aug 30 '06 #10
RobG wrote:
[...]
A bit more concise:

function makeDTGroup(id){
var node = document.getElementById(id).firstChild;
var nodeName;
var dts = [];
var dtNum = 0;
var ddGroup;
do {
if (1 == node.nodeType){
nodeName = node.nodeName.toLowerCase();
if ('dt' == nodeName){
dts.push({dt:node, dd: []});
ddGroup = dts[dtNum++].dd;
} else if (ddGroup && 'dd' == nodeName){
ddGroup.push(node);
}
}
} while( (node = node.nextSibling) )
return dts;
}


The first example works well. I haven't been able to plugu in the second
example with the showObj() function yet. I need to decipher what you have
done here :-)

Now I need to look at this to understand the methods you used.

David J.
Aug 30 '06 #11
RobG wrote:
[...]
A bit more concise:
function makeDTGroup(id){
var node = document.getElementById(id).firstChild;
[...]

Continuing on with this, your modified code works well, however I have come
accross having to process multiple DLs instead of just one with an "id". I
pretty much understand how your code works, not completely but I see what's
going on.

I have succeeded in grabbing the DLs by a unique class name, however getting
the elements into arrays after this seems problematic. I know this is
possible. I have put up a link to what I have. Any suggestions/pointers/push
are welcome in where I am failing in the method.

http://mysite.verizon.net/microweb2/newCol.html

David J.
Sep 3 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: superprad | last post by:
Is there an easy way to grab the Unique elements from a list? For Example: data = what I am looking for is the unique elements 0.4 and 0.9 with their index from the list. Probably something...
0
by: Wolfgang Lipp | last post by:
From: Lipp, Wolfgang Sent: Tuesday, 27?January?2004 13:26 <annotation> the first eleven contributions in this thread started as an off-list email discussion; i have posted them here with...
6
by: dan glenn | last post by:
hi. I'm having a problem using javasript to pass the value of a textarea (in a form) to a PHP script file. I want to code a 'preview' function into a guestbook entry page, using an HTML link...
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...
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
4
by: David | last post by:
Is there a way to find which element in an object array the keyword 'this' is acting upon? For example: function doMe(){ var aTags = document.getElementById("list").getElementsByTagName("a");...
1
by: LaughOutLoud | last post by:
Hi. I wonder is it possible to grab everything in a form by calling one javascript function and turns it into array or object? E.g. <form id="form1" name="form1" method="post" action="">...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.