2

I have this problem, a textbox is assigned to get the value of the datepicker. I have no problems doing it initially (meaning at first with no changes on the datepicker) since there's a button to be clicked to produce the hidden textbox. But the problem arises when the value of the datepicker is changed (after the textbox is generated). Here's the sample:

Datepicker:

<input type='text' name='devd' id="devd_1" class='pickdate' size='10' />

Hidden Textbox:

 <input type='hidden' name='devd_box' id='devd_box_1'/>

The button that I mentioned should only be used once. So my problem now is how to populate the hidden textbox if the value of the datepicker has changed.

You may notice the last number of the ID, that is their common reference so that it can update the right box since it has multiple datepickers/boxes.

Thanks for all the help!

P.S: The last number after the IDs are dynamically populated thus I can't just hardcode the ID of the datepicker.

Actual solution for those who need it:

$('.pickdate').on('change', function (ev) {
        var string_id = this.id;
        var lastnum = string_id[string_id.length -1];
        $('#devd_box_' + lastnum).val(this.value);
    });
Rav
  • 1,327
  • 3
  • 18
  • 32
  • 1
    Can you show your `.datepicker()` code, and how you tried using `onSelect: function()`? (based off [your comment](http://stackoverflow.com/questions/40668667/jquery-update-a-certain-textbox-on-if-datepicker-ui-is-changed#comment68568203_40668765)) – Sean Nov 18 '16 at 03:22
  • @Sean, thanks for taking time to answer this but I solved my problem now. :) – Rav Nov 18 '16 at 03:31

1 Answers1

2

Simply look for the onChange event.

$('#devd_1').on('change', function (ev) {
    $('#devd_box_1').val('Your text here');
});

This will change the text on the hidden field whenever there's a change made on the datepicker field.

Regarding the dynamic id part - Make a javascript function for it and call it to get the actual id.

Note: Please refer the question for the actual solution.

linuxartisan
  • 2,396
  • 3
  • 21
  • 40
  • 1
    I already tried that and it doesn't work though. I read this http://stackoverflow.com/questions/6471959/jquery-datepicker-onchange-event-help but I need clarification. – Rav Nov 18 '16 at 03:13
  • 1
    Try `$('#devd_box_1').val('Your text here');` – linuxartisan Nov 18 '16 at 03:15
  • 1
    And I couldn't just hard-code the id of the datepicker. The question I posted above is for only one datepicker but as I also mentioned, the module has multiple datepickers with the last number (e.g _1) dynamically populated. – Rav Nov 18 '16 at 03:15
  • 1
    I have no problems populating the textbox as I mentioned. It's more on capturing the onChange event of the datepicker. – Rav Nov 18 '16 at 03:16
  • 1
    `$('#devd_1').on('change', function (ev) {` should capture the event. – linuxartisan Nov 18 '16 at 03:16
  • Again, I tried that, but it doesn't work. jQuery Datepicker must have something going on. – Rav Nov 18 '16 at 03:17
  • Then it must have something to do with the datepicker you are using. Because it works for me. I am using `bootstrap-datetimepicker` – linuxartisan Nov 18 '16 at 03:21
  • How can I pass the value of the changed datepicker to the textbox? – Rav Nov 18 '16 at 03:26
  • 1
    Thanks @linuxartisan, at least I got hint from your answer. Much appreciated! – Rav Nov 18 '16 at 03:31
  • 1
    Glad to help. Please post your solution, so that others can benefit from it. – linuxartisan Nov 18 '16 at 03:33