Search code examples
javascriptasp.netrunatserver

runat="server" breaks my jQuery - datapicker


The runat="server" is breaking my jquery. I have two input, but for testing purpose added runat="server" in only one of those. Actually , I need to add on both.

Below you can find JS script to trigger the datetimepicker: note: dateTo has runat="server" set and tried to change the way JS trying to get its ID, but still not working.

<script>
        $(function(){
            $("#dateFrom").datetimepicker();
            $("#<%=dateTo%>").datetimepicker();
        });
</script>

Here you can find the HTML input using runat="server" or not into asp.net code.

    <tr>
        <td>
             <input type="text" id="dateFrom" name="dateFrom" value="" class="dateFrom" />
        </td>
        <td >
             <input type="text" id="dateTo" name="dateTo" runat="server" value="" class="dateTo" /> 
        </td>
    </tr>

Does anybody has any idea,hint.....? thank you


Solution

  • Use ClientID to get the generated Id of a server side control:

    $("#<%=dateTo.ClientID%>").datetimepicker();
    

    ASP.NET will generate a specific id attribute that is different from the id attribute of the server side control, so you need to use the generated value in order to access it from jQuery.