Web Analytics Made Easy -
StatCounter Evaluates surounding tags of a text - CodingForum

Announcement

Collapse
No announcement yet.

Evaluates surounding tags of a text

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Evaluates surounding tags of a text

    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?


    thanks,
    ionut

  • #2
    DOM Way:

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


    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
    }
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

    Comment


    • #3
      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
      }

      jasonkarldavis.com

      Comment

      Working...
      X