Search code examples
javascriptcheckboxdynamics-crm-2011xrm

Clear textbox in Javascript checking against checkbox


I have some javascript controlling the logic on an MS dynamics 2011 form.

When I click a checkbox (checked by default) There is a textbox which allows you to input data. When I uncheck this box, the textbox disappears. However, When I recheck the checkbox, the textbox reappears (as desired) but still has any text that was inputted previously, still stored. How can I ensure the textbox is set to null? Code snippet included.

Thanks.

function SetupForm() {
     debugger;
     HA_OtherText();
 }

 function HA_OtherText() {
     Xrm.Page.getAttribute("ha_other").getValue();
     if (Xrm.Page.getAttribute("ha_other").getValue() == true) {
         Xrm.Page.ui.controls.get("ha_othertext").setVisible(true);
     } else {
         Xrm.Page.ui.controls.get("ha_othertext").setVisible(false);
     }
 }

Solution

  • All you're doing is setting the field to visible or visible, you'll need to set the field value to null if you want to "clear" it:

    function HA_OtherText()
    {
      Xrm.Page.getAttribute("ha_other").getValue();
      if (Xrm.Page.getAttribute("ha_other").getValue() == true) 
      {
        Xrm.Page.ui.controls.get("ha_othertext").setVisible(true);
      }
      else
      {
        Xrm.Page.ui.controls.get("ha_othertext").setVisible(false);
        Xrm.Page.getAttribute("ha_othertext").setValue(null);
      }
    }