I am new to javascript and I am writing a simple site, I having a problem changing the content of a div when user click any of the menu link. So far I've tried the following code below:
When user click the menu the div text content will change. For example when user click the NEWS menu "<div class="paneltitle" id="contenttitle">Home</div>" should change the content from HOME to NEWS
HTML
<div id="wrapper">
<div id="content">
<div class="menu">
<ul>
<li><a href="#" onclick="changetitle_home()">Home</a></li>
<li><a href="#" onclick="changetitle_news()">News</a></li>
<li><a href="#" onclick="changetitle_contact()">Contact</a></li>
<li><a href="#" onclick="changetitle_about()">About</a></li>
</ul>
</div>
<div class="body_content">
<div class="paneltitle" id="contenttitle">Home</div>
</div>
</div>
CSS
ul {
list-style-type: none;
margin: 0;
padding: 0;
padding: 5px;
}
a {
display: block;
width: 60px;
text-decoration: none;
}
.menu {
float: left;
}
.body_content {
float: left;
height: 380px;
background: red;
width: 620px
}
.paneltitle {
background: gray;
width: 98.5%;
padding: 5px;
}
JS
function changetitle_home() {
document.getElementById("contenttitle").innerHTML = "Home";
}
function changetitle_news() {
document.getElementById("contenttitle").innerHTML = "News";
}
function changetitle_contact() {
document.getElementById("contenttitle").innerHTML = "Contact";
}
function changetitle_about() {
document.getElementById("contenttitle").innerHTML = "About Us";
}
Check my JSfiddle here http://jsfiddle.net/453j50uL/1/
onclickIf you set jsfiddle option tono-wrap in bodyit will work.