Question

I am looking for a simple JavaScript example that updates DOM.
Any suggestions?

Was it helpful?

Solution

Here is a short pure-javascript example. Assume you have a div with the id "maincontent".

var newnode = document.createTextNode('Here is some text.');
document.getElementById('maincontent').appendChild(newnode);

Of course, things are a lot easier (especially when you want to do more complicated things) with jQuery.

OTHER TIPS

@Ravi

Here's working example of your code

<html>
    <head>
    <title>Font Detect please</title>

    <script src="prototype.js" type="text/javascript"></script>
    <script type="text/javascript">
        function changeTD()
        {
            $('Myanmar3').innerHTML = 'False';         
        }
    </script>
    </head>
    <body>              

        <table border="1">
        <tr><td>Font</td><td>Installed</td></tr>
        <tr><td>Myanmar3</td><td id="Myanmar3">True</td></tr>
        </table>        

        <a href="javascript:void(0);" onclick="changeTD();">Click Me</a>

    </body>
</html>

You'll notice that I added a little link that you have to click to actually make the change. I thought this might make it easier to try out for real.

I believe that this tutorial on jQuery has an example that might help you: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery

A more specific question might give more helpful results, but here's a simple pair of snippets that shows and later updates text in a status container element.

// give some visual cue that you're waiting
container.appendChild( document.createTextNode( "Getting stuff from remote server..." ) );

// then later...        
// update request status    
container.replaceChild( document.createTextNode( "Done." ), container.firstChild );

<html>
    <head>
    <title>Font Detect please</title>

    <script src="prototype.js" type="text/javascript"></script>
    <script type="text/javascript">
        $('Myanmar3').update('False');         
        $('Myanmar3').innerHTML;        
    </script>
    </head>
    <body>      

        <table border="1">
        <tr><td>Font</td><td>Installed</td></tr>
        <tr><td>Myanmar3</td><td id=Myanmar3>True</td></tr>
        </table>        

    </body>
</html>

I have a simple code like that above and am trying to change the result True to false via Javascript using Prototype. What might I be doing wrong?

Edit: Got it. I didn't call it. :D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top