Change content in another Div using Javascript
P粉769045426
P粉769045426 2023-09-05 23:33:53
0
1
554
<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>
P粉769045426
P粉769045426

reply all(1)
P粉232793765


Changed code (working)

<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="showText('text-1')">
                <div class="home-side-menu">Mission Statement</div>
            </a>
            <a href="#" id="text-2" onclick="showText('text-2')">
                <div class="home-side-menu">Letter-writing</div>
            </a>
            <a href="#" id="text-3" onclick="showText('text-3')">
                <div class="home-side-menu">Peace Ideas</div>
            </a>
            <a href="#" id="text-4" onclick="showText('text-4')">
                <div class="home-side-menu">Power Of Love</div>
            </a>
        </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 showText(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>

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template