How to know span content???

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

    #1

    How to know span content???

    I shoul want to know the content o a certain span:


    *************** *************** *************** *************** ************

    <html>
    <head>
    <title>Document o senza titolo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
    <script type="text/javascript">
    function return_span_val ue(){
    var s = document.getEle mentById("x");
    alert(x.nodeNam e);
    alert(x.nodeVal ue);
    }
    </script>
    <body>
    <span id="x">Hello world!</span>
    <input type="button" value="Click to know span value"
    onClick="return _span_value();" >
    </body>
    </html>

    *************** *************** *************** *************** ************

    It should return "Hello world!" doesn't it?

    Any help appreciated.

    Regards.

    --
    Fabri
    (Non è tuttu oru chiddu chi stralluci, né tuttu veru chiddu ca si dici)
  • Alvaro G Vicario

    #2
    Re: How to know span content???

    *** Fabri wrote/escribió (Thu, 17 Mar 2005 10:03:56 +0100):[color=blue]
    > function return_span_val ue(){
    > var s = document.getEle mentById("x");
    > alert(x.nodeNam e);
    > alert(x.nodeVal ue);
    > }[/color]

    Try:

    alert(x.innerHT ML);


    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- Don't e-mail me your questions, post them to the group
    --

    Comment

    • Michael Winter

      #3
      Re: How to know span content???

      Fabri wrote:

      [snip]
      [color=blue]
      > var s = document.getEle mentById("x");
      > alert(x.nodeNam e);
      > alert(x.nodeVal ue);[/color]

      I assume you meant s.node<...>

      [snip]
      [color=blue]
      > It should return "Hello world!" doesn't it?[/color]

      No. The SPAN element itself doesn't contain the text; it has a child
      text node and /that/ contains the text:

      /* Check for getElementById support before using the method. */
      if(document.get ElementById) {
      var s = document.getEle mentById('x'), tN;

      /* Check that s is a valid reference, and that it
      * has non-null object reference in firstChild.
      */
      if(s && (tN = s.firstChild)) {
      /* tN now references the text node within the SPAN.
      * Displaying tN.nodeName should reveal '#text'.
      *
      * Both the data and nodeValue properties
      * of a text node return the text within.
      */
      alert(tN.data); // 'Hello world!'
      }
      }

      Hope that helps,
      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Fabri

        #4
        Re: How to know span content???

        Michael Winter wrote:
        [color=blue]
        > alert(tN.data);[/color]

        Also

        alert(tN.nodeVa lue);

        works.

        Regards.

        --
        Fabri
        (Non è tuttu oru chiddu chi stralluci, né tuttu veru chiddu ca si dici)

        Comment

        • J. J. Cale

          #5
          Re: How to know span content???

          <snip>[color=blue]
          > both the data and nodeValue properties
          > * of a text node return the text within.
          > */[/color]

          and with that Michael would you be kind enough to explain the difference and
          when to use one or the other? I find nothing definitive. TIA
          jimbo








          ael Winter[color=blue]
          > Replace ".invalid" with ".uk" to reply by e-mail.[/color]


          Comment

          • RobB

            #6
            Re: How to know span content???

            Fabri wrote:[color=blue]
            > I shoul want to know the content o a certain span:
            >
            >
            >[/color]
            *************** *************** *************** *************** ************[color=blue]
            >
            > <html>
            > <head>
            > <title>Document o senza titolo</title>
            > <meta http-equiv="Content-Type" content="text/html;[/color]
            charset=iso-8859-1">[color=blue]
            > </head>
            > <script type="text/javascript">
            > function return_span_val ue(){
            > var s = document.getEle mentById("x");
            > alert(x.nodeNam e);
            > alert(x.nodeVal ue);
            > }
            > </script>
            > <body>
            > <span id="x">Hello world!</span>
            > <input type="button" value="Click to know span value"
            > onClick="return _span_value();" >
            > </body>
            > </html>
            >
            >[/color]
            *************** *************** *************** *************** ************[color=blue]
            >
            > It should return "Hello world!" doesn't it?
            >
            > Any help appreciated.
            >
            > Regards.
            >
            > --
            > Fabri
            > (Non è tuttu oru chiddu chi stralluci, né tuttu veru chiddu ca si[/color]
            dici)

            For starters - don't put a <script> between <head> & <body> tags.

            It may not be a good idea to make any assumptions about what may be
            appended to that span - could be nested elements, could be whitespace
            nodes...innerHT ML will return rich content, but is non-standard. Here's
            another possibility.

            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
            <html>
            <head>
            <title>Document o senza titolo</title>
            <meta http-equiv="Content-Type" content="text/html;
            charset=iso-8859-1">
            <script type="text/javascript">

            if (document.ELEME NT_NODE == null)
            {
            document.ELEMEN T_NODE = 1;
            document.TEXT_N ODE = 3;
            }

            function normalizeString (s)
            {
            return s.replace(/\s\s+/g, " ").replace(/^\s*|\s*$/g, "");
            }

            function getTextValue(el )
            {
            var i;
            var s = "";
            for (i = 0; i < el.childNodes.l ength; i++)
            if (el.childNodes[i].nodeType == document.TEXT_N ODE)
            s += el.childNodes[i].nodeValue;
            else if (el.childNodes[i].nodeType == document.ELEMEN T_NODE &&
            el.childNodes[i].tagName == "BR")
            s += " ";
            else s += getTextValue(el .childNodes[i]);
            return normalizeString (s);
            }

            function return_span_val ue(span_id)
            {
            var s = document.getEle mentById(span_i d);
            alert('span id: ' + s.id);
            alert('span nodeName: ' + s.nodeName);
            alert('s "text value": ' + getTextValue(s) );
            }

            </script>
            </head>
            <body>
            <span id="x1">Hello world!</span>
            <input type="button" value="Click to know span value"
            onClick="return _span_value('x1 ');">
            <span id="x2"><a href="#null"><b >H</b>ello world!</a></span>
            <input type="button" value="Click to know span value"
            onClick="return _span_value('x2 ');">
            </body>
            </html>

            //getTextValue courtesy Mike Hall
            //http://www.brainjar.co m/dhtml/tablesort/default4.asp

            According to webreference:

            The Document Object Model includes two relevant properties you can use:
            data and nodeValue. You can use these properties to modify the content
            of text nodes only. Both properties are set to the text node's value
            and are fully interchangeable .

            Comment

            • Michael Winter

              #7
              Re: How to know span content???

              J. J. Cale wrote:
              [color=blue]
              > <snip>
              >[color=green]
              >> * both the data and nodeValue properties
              >> * of a text node return the text within.
              >> */[/color]
              >
              > and with that Michael would you be kind enough to explain the difference and
              > when to use one or the other?[/color]

              As far as I know, there isn't one. Both Node and CharacterData/Text
              are fundamental interfaces (so they should be implemented by all
              conforming user agents) and both data and nodeValue share the same
              exception behaviour. Just pick which one you prefer. I use data
              because it's unique and so it should be obvious (if not already) that
              code is dealing with a text node.

              Mike

              --
              Michael Winter
              Replace ".invalid" with ".uk" to reply by e-mail.

              Comment

              Working...