Home » Code

Javascript innerHTML

Using Javascript, you can change the content of a certain tag just through basic javascript browser functions. This can be useful for navigation systems, content on a page to display, etc.

Here is an example:

<script type="text/javascript">
function setContent(id,content_id){
var content_box = document.getElementById(id);
var new_content = document.getElementById(content_id);
content_box.innerHTML = new_content.innerHTML;
}
</script>

This is a basic javascript function to set the innerHTML of one id to the innerHTML of another. Now lets make the html in the body of the page.

<p><a href="#" onclick="setContent('box1','box2');">Click here to change the content!</a></p>
<div id="box1">Content #1.</div>
<div id="box2" style="display:none">Content #2.</div>

When you click the link the content will change to #2.

Here is a good example for using navigation bars:

<p><a href="#" onclick="setContent('box','link_1');">Link 1</a> | <a href="#" onclick="setContent('box','link_2');">Link 2</a> | <a href="#" onclick="setContent('box','link_3');">Link 3</a></p>
<div id="box">Navigation details displayed here...</div>
<div id="link_1" style="display:none">Link 1 details.</div>
<div id="link_2" style="display:none">Link 2 details.</div>
<div id="link_3" style="display:none">Link 3 details.</div>

You can view a live working version of these here!

0

Popularity: 1% [?]

Share/Bookmark this!

Leave a reply

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

Side Notes

This entry was posted by on April 30, 2009 at 4:46 PM and filed under Code category.

You can add your comments or trackback from your own site. To keep you updated to the latest discussion, you can subscribe to these comments via RSS.

Recent Entries

Pages

Recent Comments

Resources

Questions & Answers

Just started! Have not answered any questions.

Tag Cloud

background body CSS database date dropdown email error_reporting favicon file filesize form format function global hosting HTML image Javascript limit link linkbar mysql numbers online option ordinalize pagination pattern photoshop PHP resize rows script search snowing stylesheet switch table thumbnail time timestamp upload validate variables

Sponsors