Change content in another Div using Javascript
P粉769045426
2023-09-05 23:33:53
<p>I want to change another div (<code>id="new-text"< by clicking on the link container and using a JavaScript function (<code>show-text()</code>) /code>) content code>)</p>
<p>
<pre class="brush:html;toolbar:false;"> <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> </pre>
</p>
Changed code (working)
Inside
onclick="show-text()
, pass a valid parameter (matching your IF condition), such as "text-1" or "text-2"..should be
So does the rest.
And use
===
(strict equality check) in Script for comparison. CheckJs comparison operators