2

I need to show the jQuery UI Datepicker plug in for Html.TextBox, how can I do this?

jQuery Code:

<script>
    $(function () {
        $("#datepicker").datepicker();
    });
</script>

<div class="demo">
     <p>Date: <input type="text" id="datepicker" style = "width:150px"/></p>
</div>

The above code gives me a text box control in my MVC view where I can click on the control and it pops up a calendar control, it works well, but what I want is to reproduce the same behavior for a Html.Textbox I am using later in the code, like this:

<%= Html.TextBox("Date", "", new { style = "width:150px"})%> 

How can I get the jQuery UI Datepicker plugin to work in the above line of code?

1 Answer 1

6

Add class="datepicker" to your htmlAttributes

  <script>
 $(function () {
    $(".datepicker").datepicker();
 });
 </script>

 <div class="demo">
     <p>Date: <input type="text" id="datepicker" style = "width:150px"/></p>
 </div>
  <%= Html.TextBox("Date", "", new { style = "width:150px", @class = "datepicker"})%>
Sign up to request clarification or add additional context in comments.

6 Comments

You shouldn't have 2 identical ids on the same page.
I tried this before. It did not work. Now I commented the first text box <input type="textbox".....> and then Html.TextBox started working. Is it because datepicker can be used by only one at a time...?
@Ayman Safadi do you realy think I belive a down vote for that?
@ZVenue use class instead of id, you can have multiple elements with same class name.
How can I make the Html.TextBox take only input from the calendar and not typing?
|

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.