Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do I access the text within an asp.net mvc textbox via JavaScript?

Basically, I have 2 textboxes which you can see below and I am using the javascript below to access the text selected via the date-picker and outputted to the textboxes but what I have done so far, seems to return null values.



Code:

C#
@Html.Label("Start", "Start Date:")
@Html.TextBox("Start", string.Empty, new {@id = "Start2", @class = "datepicker"})
@Html.Label("endd", "End Date:")
@Html.TextBox("endd", string.Empty, new {@id = "End2", @class = "datepicker"})
<input type="submit" value="Apply" id ="DateSelected" />


<script type="text/javascript">
  $('.datepicker').datepicker();
$("#DateSelected").onclick(function () {
            var value1 = document.getElementById('<%=Start2.ClientID%>').value;
            var value2 = document.getElementById('<%=End2.ClientID%>').value;

</script>
Posted
Updated 23-Oct-14 2:29am
v2
Comments
First in HTML, check what are the ids of the labels.

1 solution

First of all I am anticipating that you are using jquery date picker.
So I would recommend you to use ID for both instead of class. Still this should work out. As you are using date picker in textbox, that value can be obtained through..
C#
$(".datepicker").datepicker(
{
    onSelect: function()
    {
        var dateObject = $(this).datepicker('getDate');
    }
});

This returns back the date selected. Please try with this if this works. Else post back your queries.
Thanks
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900