Search code examples
jqueryhtmlasp.net-mvc-5

Hide/Show Div on button click using Jquery


This is supposed to be my first Jquery code that I am writing. I have used this and many more examples to make the simplest jquery code to display Hello on Button Click(W3Schools worth mentioning). I am trying to show a div that contains the Hello in it on a button click.

<div>
<input type="button" id="btn" class="btn btn-default" value="click me">
</div>

<div id="Create" style="visibility:hidden">
Hello
</div>

@section Scripts{
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(document).ready(function () {
    $(btn).click(function () {
        $(Create).show();

    });
});
</script>
}

I have tried writing the Script code many places like in the head, after the Scripts.Render, before it. I am not really sure where i should place the Jquery code.

I have this code appended to a MVC5 application. This code is written for learning purpose. I think the other code in the View is irrelevant for the working of the Jquery.


Solution

  • <div>
    <input type="button" id="btn" class="btn btn-default" value="click me">
    </div>
    
    <div id="Create" style="display:none">
    Hello
    </div>
    
    @section Scripts{
    @Scripts.Render("~/bundles/jqueryval")
    <script type="text/javascript">
    $(document).ready(function () {
        $("#btn").click(function () {
            $("#Create").toggle();
        });
    });
    </script>
    }