Replace div onclick

Hello,

When a user clicks on a link, I want to REPLACE the contents of a div with new content.

I don't want to HIDE the div, I want to replace it because it contains JavaScript variables.
<div id="scriptVars">
<script>
# script goes here
</script>
</div>

Open in new window

LVL 16
hankknightAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LakioCommented:
document.getElementById('scriptVars').innerHTML = 'new content'
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hkorerCommented:
I need more clarity on how to use it, where is the link? where do i put the script? where do i put the contents of my div?
0
LakioCommented:
Try this and ask if you need more info.
<html>
<head>
	<title>Change div content</title>
	<style>
		/* this is just to add color and look */
		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 goes here
		</script>
	</div>
	<a href="#" onclick="changeDivContent( 'scriptVars2', 'This is the new Content' )">Click here to change the content of the div below</a><a href="#" onclick="changeDivContent( 'scriptVars2', 'This is the new new Content' )">Click here to change the content of the div below again</a>
	<div id="scriptVars2">
		Script is here!
		<script>
			// # script goes here
		</script>
	</div>
	<a href="#" onclick="changeDivContent( 'bob', 'This is the new Content' )">Click here to change the content of the div below</a><a href="#" onclick="changeDivContent( 'bob', 'This is the new new Content' )">Click here to change the content of the div below again</a>
	<div id="bob">
		Script is here!
		<script>
			// # script goes here
		</script>
	</div>
</body>
</html>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.