Search code examples
javascripthtmlcssdomhidden

how to display a hidden button after document.write('')?


I'm exploring a mixture of codes in javascript, HTML, CSS, and bootstrap. I tried to clear a page through a button click--> document.write(''), then kinda tried to repaint another button. Kindly explain where I've gone wrong, if this is a good practice or not. Sorry, if this is silly. On clicking button one, two things happen: 1) Page is cleared 2) The visibility property of the second button is changed by changing its class name (invoking the new class name's style property)

<style type="text/css">
.bn{visibility:hidden;}
.btn{visibility:visible;}
</style>
<script type="text/javascript">
function newpg(){
            document.write('');
            document.getElementById('two').className="btn";}
</script>
</head><body>
<div class="container">
<div class="row">
<div class="col-md-6">    <!--some content-->
<button id="one" onclick="newpg()">CLEAR &SHOW THE HIDDEN BUTTON</button>
<button class="bn" id="two">I'M HERE</button>
</div></div></div>   <!--bootstrap code inclusion-->
</body></html>

Solution

  • Using document.write('') you're actually deleting every HTML element on your page, So there remains no element to be shown next. You should use another function to just eliminate the button with id one instead of deleting everything. I suggest document.getElementById('one').style.display="none". So the code would be something like this:

    <style type="text/css">
        .bn{visibility:hidden;}
        .btn{visibility:visible;}
    </style>
    <script type="text/javascript">
        function newpg() {
                document.getElementById('one').style.display="none";
                document.getElementById('two').className="btn";
        }
    </script>
    </head>
    <body>
        <div class="container">
        <div class="row">
        <div class="col-md-6">    <!--some content-->
            <button id="one" onclick="newpg()">
                CLEAR &SHOW THE HIDDEN BUTTON
            </button>
            <button class="bn" id="two">I'M HERE</button>
        </div></div></div>   <!--bootstrap code inclusion-->
    </body>