0
<script type="text/javascript">
    $(document).ready(function () {
        $('input[name="time"]').ptTimeSelect();
    });
</script>

the above script is working on this:

<input name="time" value="" /></td>

But not working on this...

<asp:TextBox ID="time" name='time' runat="server"></asp:TextBox>

SamuraiJack
  • 5,131
  • 15
  • 89
  • 195
  • ASP.net controls translate to a HTML control, so look at the HTML source code to see what's off. (My guess is it's because you're only using the `id` of "time" and not the `name`) – Pekka Feb 22 '13 at 11:40
  • i tried it with name also it still did not work – SamuraiJack Feb 22 '13 at 11:41
  • 1
    This answer is most probably the solution to your question too: http://stackoverflow.com/a/6345700/79485 You need to use the ClientID property. – Marcel Feb 22 '13 at 11:42

3 Answers3

0

From what I remember of ASP.NET is that it modifies the ID and name of an element to ensure it's always unique. The end result of the <asp:TextBox> may be something like:

<input name="ctr_0102_time" />

The best bet is to check the element's source on the live page to determine what attributes it has. If it does have a random identifier then you should probably base it on a specific class:

<input class="time" />
<asp:TextBox CssClass="time"></asp:TextBox>

$('input.time') ...

The other thing that may cause this to break are post backs. Post backs in ASP.NET do not re-ready the document, they simply reload the page. Instead of using $(document).ready() use:

function pageLoad() { ... }
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
0

Please use following code

<script type="text/javascript">
$(document).ready(function () {
var id='<%=time.clientid%>'
    $('#'+id).ptTimeSelect();
});
 </script>

and let me know if this does not work.

Ravi
  • 310
  • 3
  • 11
  • var id = '<%=time.clientid%>' CS1061: 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'clientid' and no extension method 'clientid' accepting a first argument of type 'System.Web.UI.WebControls.TextBox' could be found (are you missing a using directive or an assembly reference?) – SamuraiJack Feb 22 '13 at 11:58
  • 1
    seems you are using c# in your server side code. please replace clientid with ClientID as it is case sensitive. – Ravi Feb 22 '13 at 12:42
  • it worked, but can you explain the code a little, i know nothing about javascripts or jquery, also if i have more than one textboxes do i have to copy paste – SamuraiJack Feb 22 '13 at 12:47
  • 1
    ASP.net dynamically generates IDs according to the name of parent control so id which appears in aspx is not always same as generated in browser. you can use control.ClientID to find out ID which will generated by framework. to find this element using jquery we need to add # sign before generated id. – Ravi Feb 22 '13 at 12:55
  • 1
    you can use classname if want to implement same jquery functionality with may textboxes like $(document).ready(function () { $('yourclassname').ptTimeSelect(); }); you need to set the same classname to each textbox for which you want to call jquery function. – Ravi Feb 22 '13 at 13:02
  • answer is good but he forgot to mention that this javascript can only be placed on as aspx page, else it wouldnt run. Like if you want to move this code to a separate js file and include it in your aspx page, it wont run. – Taha Rehman Siddiqui Feb 26 '13 at 20:58
  • @TahaRehmanSiddiqui can you please answer [this question](http://stackoverflow.com/questions/20114748/pttimeselect-jquery-timepicker-plugin-issue) – Rahman Nov 21 '13 at 08:12
0

try this:

<script type="text/javascript">
    $(document).ready(function () {
        $('[id$=time]').ptTimeSelect();
    });
</script>
RedDevil79
  • 445
  • 5
  • 13
  • CS1061: 'System.Web.UI.WebControls.TextBox' does not contain a definition for 'clientid' and no extension method 'clientid' accepting a first argument of type 'System.Web.UI.WebControls.TextBox' could be found (are you missing a using directive or an assembly reference?) – SamuraiJack Feb 22 '13 at 12:28
  • My solution is not using clientid, you may have other errors in your code. – RedDevil79 Feb 22 '13 at 12:32