473,698 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2377
Using DOM:

var root = document.getEle mentById("dlLis t"); // the parent...

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

for (var i = 0; i < dts.length; i++) {
var dds = dts.getElements ByTagName("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.getEle mentById("dlLis t"); // the parent...

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

for (var i = 0; i < dts.length; i++) {
var dds = dts.getElements ByTagName("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.getEle mentById("dlLis t"); // the parent...

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

for (var i = 0; i < dts.length; i++) {
var dds = dts[i].getElementsByT agName("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.getElementsB yTagName("a");
// var href = anchor.href;
}
}

//the end.

Aug 30 '06 #4
Using DOM:

var root = document.getEle mentById("dlLis t"); // the parent...

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

for (var i = 0; i < dts.length; i++) {
var dds = dts[i].getElementsByT agName("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.getElementsB yTagName("a");
// var href = anchor.href;
}
}

//the end.

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

var root = document.getEle mentById("dlLis t"); // the parent...

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

for (var i = 0; i < dts.length; i++) {
var dds = dts.getElements ByTagName("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.getEle mentById(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.t oLowerCase()){
dtGroups[dtNum] = {};
dtGroup = dtGroups[dtNum++];
dtGroup['dt'] = node;
dtGroup['dd'] = [];
currentDDGroup = dtGroup['dd'];
ddNum = 0;
} else if (currentDDGroup &&
'dd' == node.nodeName.t oLowerCase()){
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(dtGroup s).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.na me) {
msg.push(t + ': ' + obj[p].constructor.na me);
} 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="makeDT Groups('dlList' );">makeDDGroup s()</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.getEle mentById(id).fi rstChild;
var nodeName;
var dts = [];
var dtNum = 0;
var ddGroup;
do {
if (1 == node.nodeType){
nodeName = node.nodeName.t oLowerCase();
if ('dt' == nodeName){
dts.push({dt:no de, dd: []});
ddGroup = dts[dtNum++].dd;
} else if (ddGroup && 'dd' == nodeName){
ddGroup.push(no de);
}
}
} while( (node = node.nextSiblin g) )
return dts;
}
--
Rob

Aug 30 '06 #10

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

Similar topics

24
4166
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 like a Hash Table approach!! I would like to get this done without unnecessary overhead.And the list could be essentially anything strings,floats,int etc...
0
625
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 the consent of their authors. </annotation>
6
1847
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 with some javascript to grab the form's textarea value, and open another browser window that uses a php file that applies all the styles, etc. to the contents of what was in the text area (the guestbook entries allow simple HTML) and show the preview...
3
6116
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 Opera7 (it works fine in Firefox). Any ideas as to why and what I might do to fix it? I've set up a little demonstration of the problem here: http://www.frostjedi.com/terra/dev/test.html It's set up to render in quirks mode to demonstrate that it...
90
10775
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 future Python versions and let dict.keys() and dict.values() both return sets instead of lists. If d is a dict, code like: for x in d.keys():
24
4381
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 we covered was within the last week of the class and all self taught. Our prof gave us an example of a Java method used to remove elements from an array: public void searchProcess() { int outIt=0;
4
1427
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"); for (var i=0; i<aTags.length; i++) { aTags.onclick=function() { this.className = "myClass";
1
1381
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=""> <input name="fname" type="text" id="fname" /> <input name="lname" type="text" id="lname" /> <input name="email" type="text" id="email" /> </form>
10
6061
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 remove the elements with odd values from your list * and the even values from your vector.
0
8675
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8897
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7729
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6521
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.