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

DOM Help Please


Hi.

Is there a way of crawling the DOM without IDs?
I want to get from the below anchor tag, which has the SomeThing()
function, and grab the "some more text" string inside the td tags to
the left.
I would also like to grab the "Some Text" string inside the anchor
tags
along the way.

How could I do this?

Thanks!

<td><a href=javascript:SomeThing()>Some Text</a></td><td></td><td>some
more text</td>

Oct 24 '07 #1
3 1506
On Oct 23, 11:16 pm, pbd22 <dush...@gmail.comwrote:
Hi.

Is there a way of crawling the DOM without IDs?
I want to get from the below anchor tag, which has the SomeThing()
function, and grab the "some more text" string inside the td tags to
the left.
I would also like to grab the "Some Text" string inside the anchor
tags
along the way.

How could I do this?

Thanks!

<td><a href=javascript:SomeThing()>Some Text</a></td><td></td><td>some
more text</td>
There's probably a better way to write whatever it is you're doing.
However assuming you already have a reference to the anchor element
somehow, you could do:
var seccondGrab = anchorReference.innerHTML;
var firstGrab =
anchorReference.parentNode.nextSibling.nextSibling .innerHTML;
The second line will not necessarily work in all browsers. Firefox
for example sees line-breaks as text nodes and will point to those
instead of the HTML element. A crossbrowser JS framework/library may
come in handy if all this DOM traversal is really necessary.

Oct 24 '07 #2
I think it is the reference to the anchor element that i am struggling
with.
All I have right now is SomeThing(). I have tried passing
SomeThing(this) with
various attempts at traversal (this.parentElement, etc and so on).
Must I have
ID or NAME to get started?

Thanks again.

Oct 24 '07 #3
On Oct 24, 1:16 pm, pbd22 <dush...@gmail.comwrote:
Hi.

Is there a way of crawling the DOM without IDs?
I want to get from the below anchor tag, which has the SomeThing()
function, and grab the "some more text" string inside the td tags to
the left.
I would also like to grab the "Some Text" string inside the anchor
tags
along the way.

How could I do this?

Thanks!

<td><a href=javascript:SomeThing()>Some
Do not use the javascript pseudo-protocol in the href attribute, use a
real URL and add an onclick handler that returns false so that the
link isn't followed if scripting is enabled, something like:

<td><a href="noJavaScript.html" onclick="
getRowText(this);
return false;
">Some

Text</a></td><td></td><td>some
more text</td>

The whole thing might look something like:

<script type="text/javascript>

// Get all text in a table row
function getRowText(el) {
var cell = upToNode(el, 'tr');
return getText(cell);
}

// Go up DOM tree, starting from el, looking for
// an element with tagName tag.
// Return element or undefined.
function upToNode(el, tag) {
tag = tag.toLowerCase();
while (el.parentNode) {
if (el.tagName.toLowerCase() == tag) {
return el;
}
el = el.parentNode;
}
}

function getText(el) {
if (typeof el.textContent == 'string') return el.textContent;
if (typeof el.innerText == 'string') return el.innerText;
}

</script>

<table>
<tr>
<td><a href="noJavaScript.html" onclick="
alert(getRowText(this));
return false;
">Some text</a></td>
<td></td>
<td>some more text</td>
</table>
--
Rob

Oct 24 '07 #4

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

Similar topics

1
by: Numberwhun | last post by:
Hello everyone! I am trying to learn java and have run into kind of a snag. Here is the code that I have so far: ------ <begin_code> ---------- import javax.swing.*; import...
1
by: HolaGoogle | last post by:
Hi all, Please help me with the following..it's realy urgent and i tried everything i could and i can't get it work properly!! Thanks in advance. Here's what i'm trying to accomplish: in my...
0
by: s_erez | last post by:
Hi, This is a realy tricky one. I have an ASP.NET application where some pages are reading data from a DB and presenting reports. In order for the user to wait while the page is reading data from...
2
by: rked | last post by:
I get nameSPAN1 is undefined when I place cursor in comments box.. <%@ LANGUAGE="VBScript" %> <% DIM ipAddress ipAddress=Request.Servervariables("REMOTE_HOST") %> <html> <head> <meta...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
4
by: pshindle | last post by:
DB2 Team - I just downloaded and unzipped the new Fixpack 9 for DB2 ESE V8 for Windows (FP9_WR21350_ESE.exe). I then burned the unzipped Fixpack files to a CD. I proceded to install this...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
1
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and...
4
by: fatboySudsy | last post by:
Hi, I have constructed a client program that has given me some error codes that i just cannot see. I was wondering if a different set of eyes with much more experience than me could help me out. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
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:
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...

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.