Results 1 to 8 of 8

Thread: Javascript to replace content of div

  1. #1
    Join Date
    Mar 2010
    Posts
    54

    Javascript to replace content of div

    Hello everyone ,

    I am trying to create this cms in java script, but I got some problem with this procedure ..And I am unable to figure out how to replace the content of a <div> (It's not actually iframe )

    This is the explanation that I want to do , the <div> contains some text and pictures:

    <div id="div1">Somethingggggg<img src="mine.png">Somethinggggggg</div>

    I desired to replace this content "somethingggggg<img src="mine.png">Somethingggggg" with: "whatever<img src="whatever.png">whatever"

    How it would be accomplished and how to manage this in javascript


    Thanks..............

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Javascript to replace content of div

    Hi,
    I know the way through which you can do it as like, you need to only incorporate these things into your created code and it will do what you need to do but implement it with the proper place , This is code below,try this


    Code:
    document.getElementById('div1').innerHTML = 'whatever<img src="whatever.png">whatever';
    Should do it.

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Javascript to replace content of div

    There are so many ways to do it but I will suggest you go for easier and proper way through which you can do it as you want. You can also include this javascript in your page...

    <snip>

    And then adjust reference your element IDs in a much simpler fashion (well it's much simple when you have a number of these.)

    $('div1').innerHTML = 'whatever<img src="whatever.png">whatever';

  4. #4
    Join Date
    Mar 2010
    Posts
    47

    Javascript to replace content of div

    Hello friends,

    I am going to create a program for web development procedure but having some issues with that. When a user clicks on a link, I need to REPLACE the contents of a div with some other fresh content. I do not need to hide the div tag or something like that, I just want to replace this because it have some JavaScript variables.

    <div id="scriptVars">
    <script>
    # script goes here
    </script>
    </div>


    Do you have any idea, how to do it !???????

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Javascript to replace content of div

    This is one of the way that you should try to implement and execute it and I will suggest you work with this below written code and if you any problem related to implementation and execution then please let me know..for further information you can post next reply ...

    Code:
    <html>
    <head>
    	<title>Change div content</title>
    	<style>
    		
    		div, a { padding:3px; margin:3px 3px 33px 3px; }
    		#scriptVars1 { border:1px solid red; }
    		#scriptVars2 { background:green; }
    		#bob { border:3px solid blue; }
    	</style>
    	<script>
    		function changeDivContent( nameOfDiv, newContent )
    		{
    			var div = document.getElementById( nameOfDiv );
    			if( div )
    			{
    				div.innerHTML = newContent;
    			}
    		}
    	</script>
    </head>
    <body>
    	<a href="#" onclick="changeDivContent( 'scriptVars1', 'This is the new Content' )">Click here to change the content of the div below</a><a href="#" onclick="changeDivContent( 'scriptVars1', 'This is the new new Content' )">Click here to change the content of the div below again</a>
    	<div id="scriptVars1">
    		Script is here!
    		<script>
    			
    		</script>
    	</div>
    	<a href="#" onclick="changeDivContent( 'scriptVars2', 'This is the new Content' )">Click here to change the content of the div following</a><a href="#" onclick="changeDivContent( 'scriptVars2', 'This is the new new Content' )">Click here to change the content of the div following now</a>
    	<div id="scriptVars2">
    		Script is here!
    		<script>
    			
    		</script>
    	</div>
    	<a href="#" onclick="changeDivContent( 'bob', 'This is the new Content' )">Just click here to change the content of the div following</a><a href="#" onclick="changeDivContent( 'bob', 'This is the newly created Content' )">Just click here to change the content of the div following now</a>
    	<div id="bob">
    		Script is here!
    		<script>
    			
    		</script>
    	</div>
    </body>
    </html>

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    The Container Content Replacement Function

    You know a four line JavaScript function commits the replacement of text.some other JavaScript tells that function that div to target and what its new content shall be. I am going to show you a container content replacement JavaScript function that can do same.

    <script type="text/javascript"><!--
    function ReplaceContentInContainer(id,content) {
    var container = document.getElementById(id);
    container.innerHTML = content;
    }
    //--></script>

    The ReplaceContentInContainer() function requires two parameters, the id of the container and the content for the container.

  7. #7
    Join Date
    May 2008
    Posts
    3,316

    Click To Replace The Div Content Example

    This is an example of content in a div which is replaced with a click on a link. Represented code that performs the above work (consider one thing to put the four-line container content replacement JavaScript function anywhere on the current page).

    Code:
    <div 
       id="example1div" 
       style="border-style:solid; 
              padding:10px; 
              text-align:center;">
    I will be replaced when you click.
    </div>
    <a href="javascript:ReplaceContentInContainer('example1div',' Yeah! You clicked!')">
    Click me to replace the content in the container.
    </a>

  8. #8
    nizam123 Guest

    Re: Javascript to replace content of div

    I am trying to create this cms in java script, but I got some problem with this procedure ..And I am unable to figure out how to replace the content of a <div> (It's not actually iframe

    ----------------------

Similar Threads

  1. JavaScript - location.replace
    By Cordaro in forum Software Development
    Replies: 6
    Last Post: 13-05-2010, 11:55 PM
  2. How to replace HTML code using javascript
    By visioneye in forum Software Development
    Replies: 6
    Last Post: 11-05-2010, 11:38 PM
  3. Find and Replace in JavaScript String
    By Wilbur in forum Software Development
    Replies: 5
    Last Post: 15-12-2009, 02:23 PM
  4. How to use hashtable to replace content
    By RasMus in forum Software Development
    Replies: 3
    Last Post: 27-07-2009, 10:30 AM
  5. Javascript to replace plus sign
    By Jagriti in forum Software Development
    Replies: 3
    Last Post: 22-06-2009, 08:30 PM

Tags for this Thread

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
  •  
Page generated in 1,750,449,552.14674 seconds with 16 queries