Search code examples
asp.netaccessibility

How to add (hidden) label for asp:CheckBox?


I ran chrome's lighthouse accessibility test on a page I'm working on and it says

Form elements do not have associated labels

for all the checkboxes used on the page. They are in a large grid and have no labels.

Ex:

<asp:CheckBox ID="cbNotApplicable" runat="server" AutoPostBack="true" OnCheckedChanged="cbNotApplicable_CheckedChanged" onClick='javascript:needToConfirm = false;' ToolTip="Answer all questions Not Applicable" />

If I add a Text attribute, the accessibility warning goes away, but a <label> is added and visible, which I do not want.

enter image description here

Is there an alternate way to make the asp:CheckBox accessible?


Solution

  • Try this way

        <asp:Label ID="test" runat="server"
            AssociatedControlID="cbNotApplicable"> <asp:CheckBox ID="cbNotApplicable" runat="server" AutoPostBack="true" OnCheckedChanged="cbNotApplicable_CheckedChanged"
         onClick='javascript:needToConfirm = false;' ToolTip="Answer all questions Not Applicable" /> </asp:Label>

    Or, try this below link suggestions to hide label by using CSS

    https://stackoverflow.com/a/13482643/2218635