Search code examples
javascripthtmluser-interfacecheckboxyui

AUI Checkbox not showing as checked but value is true


I have a checkbox .

<aui:input name = "enable" id = "enable" type="checkbox" label= 'Enable'  onchange='handleClick(this);' />

I load the previous state of the checkbox on page load. When the previous state is checked, I do the following.

var namespace = "<portlet:namespace/>";
window.onload = function() {
    var cb = document.getElementById(namespace+ "enable");
    cb.checked = true;
    alert("state: " + cb.checked);
    //do something
};

The alert shows me the checkbox is true and it executes the code that follows. However, the view does not show the checkbox to be checked! What am I missing?


Solution

  • The bad thing or the good thing with AUI Input is, it appends Checkbox automatically to the id of input:checkbox.

    <input type="checkbox" onchange="handleClick(this);" value="true" onclick="Liferay.Util.updateCheckboxValue(this); " name="_manageorganization_WAR_manageorganizationportlet_enableCheckbox" id="_manageorganization_WAR_manageorganizationportlet_enableCheckbox" class="aui-field-input aui-field-input-choice">
    

    And in your case with your "<portlet:namespace/>";, it generates id as :

    namespace+ "enableCheckbox"
    

    So use it as :

    var namespace = "<portlet:namespace/>";
    window.onload = function() {
        var cb = document.getElementById(namespace+ "enableCheckbox");
             cb.checked = true;
            alert("state: " + cb.checked);
             //do something
    }; 
    

    And it will work.