abdul abdul - 2 months ago 6
HTML Question

How to show a div when a particular button is clicked,

This my js code:



  • i want to click first button to call a new class,it works but, every
    button doing same work. when i click second button this also calling
    a new class.please help me to solve this.

    <script>
    $(document).ready(function(){
    $("button").click(function(){ /*button click event*/
    $(".new").toggle(); /*new class calling */
    });
    });
    </script>


    this my html code:

    <button>first</button><br>
    <button>second</button><br>
    <button>third</button><br>

    <div class="new" style = "display: none;">hi.</div> <!--i need to call only this class-->
    <div class="old"><a href="www.google.com">hello.</a></div>


Answer

give id's to the buttons, n call those accordingly like

$("#first").click(function(){      
     $(".new").toggle();              
});