Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2002
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 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
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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"

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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
    }



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •