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