How to 'untag' an element?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Razzbar

    #1

    How to 'untag' an element?

    What is the preferred method of removing the tagging of an element
    without
    removing the text? IOW, how do I turn this

    <span>I want to keep this <strong>and this, too</strong> but not
    strongly</span>

    into this..

    <span>I want to keep this and this, too but not strongly</span>

    ....?

    Hey, don't tell me to used notepad! :) I mean I want to do it
    dynamically,
    on the fly, in Javascript.

    I rekon I can use removeNode() to remove the <strong> element and save
    the
    innerHTML, but then where do I put the innerHTML?

    In IE, it's possible to say "... el.outerHTML = el.innerHTML " but this
    isn't DOM, is it?

  • Martin Honnen

    #2
    Re: How to 'untag' an element?



    Razzbar wrote:
    [color=blue]
    > What is the preferred method of removing the tagging of an element
    > without
    > removing the text? IOW, how do I turn this
    >
    > <span>I want to keep this <strong>and this, too</strong> but not
    > strongly</span>
    >
    > into this..
    >
    > <span>I want to keep this and this, too but not strongly</span>[/color]

    With IE (and other browser implementing innerText) you could do
    spanElement.inn erText = spanElement.inn erText;
    [color=blue]
    > In IE, it's possible to say "... el.outerHTML = el.innerHTML " but this
    > isn't DOM, is it?[/color]

    With full W3C DOM Level 2 range support as Mozilla has it is easy:

    var span = document.getEle mentById('spani d');
    var range = document.create Range();
    range.selectNod eContents(span) ;
    var textContent = range.toString( );
    var textNode = document.create TextNode(textCo ntent);
    range.deleteCon tents();
    span.appendChil d(textNode);

    With DOM Level 3 Core textContent as new Mozillas have it you could
    simply set
    spanElement.tex tContent = spanElement.tex tContent

    If you want to implement it with DOM Level 1 or 2 Core stuff then walk
    the descendants of an element to collect the text content, then remove
    all child nodes and append a single newly created text node with the
    collected text.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    • Tomasz Cenian

      #3
      Re: How to 'untag' an element?

      Martin Honnen napisał(a):
      [color=blue]
      > [snip][/color]


      He could also use:

      sp=document.get ElementById('sp anId');
      sp.innerHTML=sp .innerHTML.repl ace(/<\/*[^>]+>/ig,'');

      The method would be widely supported even though 'innerHTML' is
      officially not part of a standard.


      --
      tomasz cenian tcenian at wa dot home dot pl
      :::: :: : : http://cenian.boo.pl : : :: ::::

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: How to 'untag' an element?

        Razzbar wrote:
        [color=blue]
        > What is the preferred method of removing the tagging of an element
        > without removing the text? IOW, how do I turn this
        >
        > <span>I want to keep this <strong>and this, too</strong> but not
        > strongly</span>
        >
        > into this..
        >
        > <span>I want to keep this and this, too but not strongly</span>
        >
        > ...?[/color]

        I'd prefer this (untested, much feature testing omitted):

        /**
        * Returns the text content of a document node.
        *
        * @author (C) 2005 Thomas Lahn <dhtml.js@Point edEars.de>
        * @argument Node oNode
        * @return type string
        * The text content of @{(oNode)}.
        */
        function getTextContent( oNode)
        {
        var text = "";

        if (oNode)
        {
        // W3C DOM Level 3
        if (typeof oNode.textConte nt != "undefined" )
        {
        text = oNode.textConte nt;
        }

        // W3C DOM Level 2
        else if (oNode.childNod es && oNode.childNode s.length)
        {
        for (var i = oNode.childNode s.length; i--;)
        {
        var o = oNode.childNode s[i];
        if (o.nodeType == ((Node && Node.TEXT_NODE) || 3))
        {
        text = o.nodeValue + text;
        }
        else
        {
        text = getTextContent( o) + text;
        }
        }
        }

        // proprietary: IE4+
        else if (typeof oNode.innerText != "undefined" )
        {
        text = oNode.innerText ;
        }
        }

        return text;
        }

        /**
        * Sets the text content of a document node.
        *
        * @author (C) 2005 Thomas Lahn <dhtml.js@Point edEars.de>
        * @argument Node oNode
        * @return type boolean
        * <code>true</code> if successful, <code<false</code> otherwise.
        */
        function setTextContent( oNode, sContent)
        {
        var result = false;

        if (oNode)
        {
        // W3C DOM Level 3
        if (typeof oNode.textConte nt != "undefined" )
        {
        oNode.textConte nt = sContent;
        result = (oNode.textCont ent == sContent);
        }

        // W3C DOM Level 2
        else if (oNode.removeCh ild
        && oNode.firstChil d
        && oNode.appendChi ld
        && document.create TextNode)
        {
        while (oNode.firstChi ld)
        {
        oNode.removeChi ld(oNode.firstC hild);
        }

        result = !!oNode.appendC hild(document.c reateTextNode(s Content));
        }

        // proprietary: IE4+
        else if (typeof oNode.innerText != "undefined" )
        {
        oNode.innerText = sContent;
        result = (oNode.innerTex t == sContent);
        }
        }

        return result;
        }

        setTextContent(
        referenceToSpan Element,
        getTextContent( referenceToSpan Element));
        [color=blue]
        > In IE, it's possible to say "... el.outerHTML = el.innerHTML " but this
        > isn't DOM, is it?[/color]

        *All* client-side manipulation of a document element tree is based on a
        DOM (Document Object Model). The method you are mentioning is simply not
        defined in the W3C DOM (Level 2+), but in proprietary DOMs, the IE4 DOM
        and the Gecko DOM.


        PointedEars

        Comment

        Working...