Change Content when clicked

Discussion in 'JavaScript' started by instant87, Jun 3, 2009.

  1. #1
    Very simple Javascript question - how would I make so when a user clicks a link, some text changes on the page without reloading it?
     
    instant87, Jun 3, 2009 IP
  2. James Barcellano

    James Barcellano Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    
    <script type="text/javascript">
    function changetext(text, id) {
        document.getElementById(id).innerHTML = text;
    }
    </script>
    
    <span onclick="changetext('newtext', 'myid')">CLICK HERE</span>
    
    <span id="myid">A line of text</span>
    
    HTML:
     
    James Barcellano, Jun 3, 2009 IP
  3. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #3
    James Barcellano has given the correct code for a static change.
    But if you want the text to be loaded from the database you will have to use AJAX.
    Reply if you are in need of such code.
     
    Unni krishnan, Jun 4, 2009 IP