0

I have the following code:

    function showDatePicker(){
        var isEnabled=#{bean.isEnabled()};
        if (isEnabled){
            jQueryForDatepicker(".calendar").datepicker('enable');
            jQueryForDatepicker(".calendar").datepicker({
                  changeMonth: true,
                  changeYear: true,
                  showOn: "button",  
                  dateFormat:'yy-mm-dd',
                  buttonImage: "../resources/calendar.gif",      
                  buttonImageOnly: true,      
                  buttonText: "Select date",
                  beforeShow: function (input, inst) {
                      var rect = input.getBoundingClientRect();
                        setTimeout(function () {
                            inst.dpDiv.css({ marginTop: -input.offsetHeight + '10' , marginLeft: input.offsetWidth + '10' });
                        }, 0);
                  }
          });
        } else {
            jQueryForDatepicker(".calendar").datepicker('disable');
        }
    }       

The function is called from an ajax event, that is tied to a drop-down list. The drop-down contains the "enable", "disable" values, and sets the attribute in the managed bean. My problem is that, in the javascript function the var isEnabled is only set when the page is loaded. Initially is set to false, after that, if I change the value in drop-down, the variable in the javascript doesn't get updated, even though my bean sends the correct value back. So if it loads with false value, it stays that way even though it should update to true, or vice-versa.

att1la
  • 23
  • 4

2 Answers2

0

I think you are doing this completely wrong. This code is triggered only when the page is loaded. If you are not refreshing the page or adding again that script in the dom, then there is no way you can use it on the client. You should go for a completely different solution.

What you should do is add js callbacks to the function.

knaos
  • 820
  • 7
  • 13
  • the function is triggered each time the drop-down is changed, there is an ajax call, the only problem is that the variable stays in the way is originally read. doesn't get updated.. – att1la Dec 14 '16 at 09:25
  • Thats because you are declaring it from the backend, or server. Declare it from the front-end instead :) – knaos Dec 14 '16 at 09:29
0

isEnabled variable is binding your bean but this script is not run again. Try call function again showDatePicker() after you make change bean value. I think you should create a function with prototype as showDatePicker(isEnabled ){...}