View Full Version : Evaluates surounding tags of a text

07-03-2002, 03:21 PM
I need a script that evaluates the tags that suround a text :
<p><font color="black">text before link<a href="#">link</a>text after link</font></p>
Concrete: I need to know if "link" text is suround only by tags or it has other texts nea him , like "text before link" or "text after link".
Is there any script made for this or has somebody a goof ideea about how to manipulate the text to find out where tags are present there?


07-03-2002, 03:51 PM
DOM Way:

<p id="p1">
text before link
<a href="#">link</a>
text after linl

p = document.getElementById('p1');
if (p.firstChild.nodeName != 'a')
{ //there is something before the link

if (p.lastChild.nodeName !='a')
{ //there is something after the link

07-03-2002, 04:23 PM
You can also make use of previousSibling/nextSibling:

var testLink = document.getElementById('p1').getElementsByTagName('a').item(0);

if (testLink.previousSibling) {
// node before it - could be an empty text node though if indented and in Gecko
// previousSibling && previousSibling.nodeType != Node.TEXT_NODE
//would take care of that issue

if (testLink.nextSibling) {
// node after it, same issues as before