0

becouse textboxes are stored in viewstate i cannot use this JS code

<script type="text/javascript">
$(function() {
    $.datepicker.setDefaults($.datepicker.regional["sl"]);
    $(".inputTypeDate").datepicker({
        dateFormat: "dd.mm.yy",
        changeMonth: true,
        changeYear: true
    });
});
</script>

becouse code of textboxes are not visible in source code.

so i try to use this with onlcick event.

html example:

<p>Date: <input type="text" id="datepicker" onClick="$(function() {$( '#datepicker' ).datepicker({changeMonth: true,    changeYear: true});});"></p>

which works but only when i click second time on textbox. How to enable calendar on first click?

senzacionale
  • 20,448
  • 67
  • 204
  • 316

2 Answers2

4

If you are having multiple textboxes with the same id it will be invalid.

Add a class name for the textboxes and bind the handler to those objects. Something like

$(function(){
    $("input:text.inputTypeDate").datepicker({
        dateFormat: "dd.mm.yy",
        changeMonth: true,
        changeYear: true
    });
});


<input type="text" class="inputTypeDate" />

Edit

I don't know why you are not able to view the element in the source code. Are you dynamically generating the element(AJAX response). If it is a dynamically created element then you will need to add the handlers using a plugin like livequery

rahul
  • 184,426
  • 49
  • 232
  • 263
  • yes this works if you can see source code but in in my example textbox is stored in viewstate so jquery can not see it in source code. only solution is onclick event as i can see. – senzacionale Jan 10 '11 at 08:54
  • `textbox is stored in viewstate`. Can you explain what you mean by this? – rahul Jan 10 '11 at 09:04
  • aspx has viestate and if i use textbox.visible= false and then i change to true this is stored in viewstate. component is visible in monitor but if you go to look at source code you can not find it. – senzacionale Jan 10 '11 at 09:15
  • that should not be problem as you will be initialising your textbox to show datepicker on document.ready. Should work as long as you are not doing any ajax calls – Krishna Chytanya Jan 10 '11 at 09:23
  • it is the same not working. i think jquery mnust see that element in source code. can i use element onclick event? – senzacionale Jan 10 '11 at 10:33
  • i create textbox elements programatically vith visible i hide it and show it. and if i use visible for showing and hidding that means that this elements will be stored in viewstate and you can not see it in source code. So you said livequery? – senzacionale Jan 10 '11 at 11:25
  • YOU SAVED MY DAY! http://www.vancelucas.com/blog/jquery-ui-datepicker-with-ajax-and-livequery/ THY AGAIN! – senzacionale Jan 10 '11 at 11:51
0

Or you could try by changing the click event handler of the text box as below.

$(function(e) {e.preventDefault();$( '#datepicker' ).datepicker({changeMonth: true,    changeYear: true}).show();});

But I would suggest that you follow the technique specified by Rahul if possible

Krishna Chytanya
  • 1,398
  • 1
  • 8
  • 12