0

I am building an Asp.net MVC app and would like to add a datepicker.

I have this code in my form in the view

@Html.LabelFor(b => b.Date)
@Html.EditorFor(b => b.Date, new {id = "news_date"})

and I would like to add this javascript to it to enable to datapicker:

$(document).ready(function () {
        $("#news_date").datepicker({ dateFormat: 'dd/mm/yy' });
});

however when the application is run it shows the above code as text on the page. How do I get it to recognize it as javascript?

2 Answers 2

2

Use script tag

@Html.LabelFor(b => b.Date)
@Html.EditorFor(b => b.Date, new {id = "news_date"})
    <script  type="text/javascript">

    $(document).ready(function () {
            $("#news_date").datepicker({ dateFormat: 'dd/mm/yy' });
    });
    </script>
Sign up to request clarification or add additional context in comments.

1 Comment

have to wait 5 more mins first
1

wrap your code with script tags

<script type="text/javascript">
    $(document).ready(function () {
       $("#news_date").datepicker({ dateFormat: 'dd/mm/yy' });
    });
</script>

1 Comment

because it is simple :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.