Results 1 to 2 of 2

Thread: Change DIV contents

  1. #1
    Join Date
    Nov 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Change DIV contents

    Hi,
    I Need help changing the content of 4 DIV with with a click on a link.
    This is the script I'm using:

    Code:
    <script type="text/javascript">
    function changeContent(id,shtml) {
       if (document.getElementById || document.all) {
          var el = document.getElementById? document.getElementById(id): document.all[id];
          if (el && typeof el.innerHTML != "undefined") el.innerHTML = shtml;
       }
    }
    
    var msg1 = "message1";
    var msg2 = "message2";
    var msg3 = "message3";
    var msg4 = "message4";
    </script>
    I know how to change the content of a single DIV:
    Code:
    <a href="javascript:;" onclick="changeContent ('first',msg1)">click here</a>
    <div id="first">text to be changed</div>
    <div id="second">more text to be changed</div>
    <div id="third">some more text to be changed</div>
    <div id="fourth">even more text to be changed</div>
    How do I extend this to function on the 4 DIV with a click on click here.

    Thanks
    Marvillas1
    Last edited by Marvillas1; 11-20-2007 at 08:37 PM. Reason: clean code

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<title>Test Document</title>
    		<style type="text/css"></style>
    		<script type="text/javascript">
    			var newContent = ['Message One','Message Two','Message Three','Message Four'];
    			function changeContent(idstr) {
    				var ids = idstr.split(',');
    				for(var i = 0; i < ids.length; i++){
    					document.getElementById(ids[i]).innerHTML = newContent[i];
    				}
    			}
    		</script>
    	</head>
    	<body>
    		<div>
    			<a href="javascript:;" onclick="changeContent ('first,second,third,fourth');">click here</a>
    			<div id="first">text to be changed</div>
    			<div id="second">more text to be changed</div>
    			<div id="third">some more text to be changed</div>
    			<div id="fourth">even more text to be changed</div>
    		</div>
    	</body>
    </html>

Bookmarks

Posting Permissions

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