www.webdeveloper.com
Results 1 to 5 of 5

Thread: How to obtain and change nodeValue text?

  1. #1
    Join Date
    Sep 2007
    Posts
    3

    How to obtain and change nodeValue text?

    Trying to understand how to obtain the text of a td tag element within a table and especially how to change that text using xml DOM and javascript. here's what I have so far; any help appreciated...

    Code:
    <!DOCTYPE html PUBLIC ""
    <html xmlns="">
    
    <head>
    </head>
    
    <body>
    <table border="1">
    <tr>
    <td>some text here</td>
    </tr>
    </table>
    </body>
    
    <script type="text/javascript">
    <!-- Try several ways to write out text of the td tag ??? --> 
    document.write(document.getElementsByTagName("td").item[0]);
    document.write(document.getElementsByTagName("td").item[0].childnodes[0].nodeValue);
    var TDElement0 = document.getElementsByTagName("td").item[0];
    document.write(TDElement0);
    
    <!-- Try to change the text of the td tag ??? -->
    document.getElementsByTagName("td").item[0].childnodes[0].nodeValue = "different text here";
    
    </script>
    
    </html>

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Use after the document has loaded
    Code:
    document.getElementsByTagName("td")[0].firstChild.data = "different text here";
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Sep 2007
    Posts
    3

    Thanks!

    Yes, thanks! Not waiting for the page to load was the problem however I want the user to click on the "some text here" text and an event handler then causes the text to be changed via getElementsByTagName("td")[0].firstChild.data

    The way it stands (see new code below) the text changes to "different text here" before the user sees the original "some text here"

    Code:
    <html>
    
    <head>
    <script type="text/javascript">
    
    function load()
    {
    document.getElementsByTagName("td")[0].firstChild.data = "different text here";
    }
    
    </script>
    
    </head>
    
    <body onload="load()">
       <table border="1">
       <tr>
       <td>some text here</td>
       </tr>
       </table>
    </body>
    
    </html>

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <title>Basic HTML</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <script type="text/javascript">
    window.onload=function() {
    var o = document.getElementsByTagName("td")[0];
    o.onclick=function(){this.firstChild.data = "different text here";};
    };
    </script>
    
    <style type="text/css">
     
    </style>
    
    </head>
    <body>
    <table border="1" cellpadding="0" cellspacing="0" summary="">
    <tbody>
        <tr><td>some text here</td></tr>
    </tbody>
    </table>
    </body>
    </html>
    At least 98% of internet users' DNA is identical to that of chimpanzees

  5. #5
    Join Date
    Sep 2007
    Posts
    3
    Excellent! Thanks very much...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles