Ivan-Mark Debono
Ivan-Mark Debono

Reputation: 16300

MVC4 hide/show section using javascript

I have the following javascript embedded in my view:

<script type="text/javascript">
    var Scripts = {

        showAddressForm: function (isNew) {
            if (isNew) {
                $('#newaddress-form').show();
            } else {
                $('#newaddress-form').hide();
            }
        },
    };
</script>

The function is called on the 'onchange' event of a dropdownlist. This works fine. However, when the page is initially loaded, the form is visible. I would like it to be hidden in this part of the code:

@if (Model.Addresses.Count > 0)
{
    //Call function to hide address form   

    //List dropdownlist items here...
}

What's the correct call to do this?

Upvotes: 0

Views: 516

Answers (2)

Serge
Serge

Reputation: 6692

Why not just do...

@if (Model.Addresses.Count > 0)
{

    //Call function to hide address form   
    <script type="text/javascript">
        $(document).ready(function() {
            Scripts.showAddressForm(false);
        });
    </script>
    //List dropdownlist items here...
}

Upvotes: 1

David Hedlund
David Hedlund

Reputation: 129792

Render the form to be hidden with style="display: none;" or set it in your CSS:

#newaddress-form { display: none; }

Upvotes: 2

Related Questions