I am looking to change content of another div (id="new-text"
) by clicking on a link container and using a JavaScript function (show-text()
)
<section>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-3 col-xl-3">
<a href="#" id="text-1" onclick="show-text()">
<div class="home-side-menu">Mission Statement</div></a>
<a href="#" id="text-2" onclick="show-text()">
<div class="home-side-menu">Letter-writing</div></a>
<a href="#" id="text-3" onclick="show-text()">
<div class="home-side-menu">Peace Ideas</div></a>
<a href="#" id="text-4" onclick="show-text()">
<div class="home-side-menu">Power Of Love</div></a>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-9 col-xl-9">
<div class="text-formatting" id="new-text">
</div>
</div>
</div>
</section>
<script type="text/javascript">
function show-text(id){
if id="text-1"
document.getElementById('new-text').innerHTML="SAMPLE TEXT 1"
if id="text-2"
document.getElementById('new-text').innerHTML="SAMPLE TEXT 2"
if id="text-3"
document.getElementById('new-text').innerHTML="SAMPLE TEXT 3"
if id="text-4"
document.getElementById('new-text').innerHTML="SAMPLE TEXT 4"
}
</script>
2
Answers
The Changed Code(Working) : https://jsfiddle.net/51wbksv9/
Inside
onclick="show-text()
,pass valid arguments ( that matches with your IF conditions) like ‘text-1’ or ‘text-2’..It should be
<a href="#" id="text-1" onclick="show-text('text-1')">
and same follows rest of them.
and use
==
inside Script , for comparison. Check this for Js Comparison OperatorsYou have invalid HTML and JavaScript, so those errors must be corrected first.
See comments inline for details.