35

I want to hide a checkbox.
But also want that, when I click on label associated with corresponding checkbox, the checkbox should get checked/unchecked.

I also want that the checkbox MUST be able to be focused.

I am doing the following:

<div class="menuitem">
    <label class="checkbox"><input type="checkbox" value="valueofcheckbox" style="display:none" checked="checked">Option Text</label>
</div>

The problem with above is, I am not able to make focus the checkbox when style="display:none".

To make checkbox focusable I am doing :

$('input', '.menuitem').focus();

IF POSSIBLE, how do I make the hidden checkbox focusable?

7
  • Would adding a tabindex to the label work? Commented Jul 31, 2013 at 20:00
  • @KnowHowSolutions Erm... say that again, slowly? REMOVES it from the DOM? Really? Commented Jul 31, 2013 at 20:02
  • visibility:hidden also does not work.. i have tried it. Commented Jul 31, 2013 at 20:02
  • 2
    You want it "hidden" but you also want it to receive "focus" and get checked/unchecked? What are you trying to accomplish with all this verbose contradiction? Surely, there's a better way... try <input type="hidden" /> to store/send whatever it is. Commented Jul 31, 2013 at 20:19
  • 1
    maybe use <label for="check">some text</label><input type="checkbox" name="check" style="display: block;"/> type 'name' attribute of checkbox in 'for' attribute of label Commented Jul 31, 2013 at 20:20

6 Answers 6

28

Try setting the checkbox's opacity to 0. If you want the checkbox to be out of flow try position:absolute and offset the checkbox by a large number.

HTML

<label class="checkbox"><input type="checkbox" value="valueofcheckbox" checked="checked" style="opacity:0; position:absolute; left:9999px;">Option Text</label>
Sign up to request clarification or add additional context in comments.

3 Comments

This would be more like visibility: hidden than display: none though.
I updated your fiddle; the solution is sloppy, but focus and checking work. fiddle
Not that visibility:hidden will take it's space.
14

Elements that are not being rendered (be it through visibility: hidden, display: none, opacity: 0.0, whatever) will not indicate focus. The browser will not draw a focus border around nothing.

If you want the text to be focusable, that's completely doable. You can wrap the whole thing in an element that can receive focus (for example, a hyperlink), or allow another tag to have focus using the tabindex property:

<label tabindex="0" class="checkbox">
  <input type="checkbox" value="valueofcheckbox" style="display:none" checked="checked" />Option Text
</label>

Fiddle

In this case, the <label> tag above is actually receiving focus and everything within it will have a focus border when it's in focus.

I do question what your goal is. If you're using a hidden checkbox to internally track some sort of state, you might be better off using a <input type="hidden" /> tag instead.

19 Comments

the hidden checkbox is still not focusable in my work... can u please edit n save the fiddle to actually show the hidden checkbox focusable?
You can't make a hidden element focusable. No matter what. The browser won't draw a focus ring around nothing.
if that is true, there is no need for tabindex.. the result would be same even without tabindex=0
You can add label:focus{outline:1px solid black} to your CSS.
i also want it to be multiselect dropdown.. so i have to use checkbox.. i also use some css to display the selected items in case of multiselect dropdown.. but not showing the checkbox to end user.. Is there a way to achieve the same without loosing my checkbox..??
|
12

This two classes are borrowed from the HTML Boilerplate main.css. Although the invisible checkbox will be focused and not the label.

/*
 * Hide only visually, but have it available for screenreaders: h5bp.com/v
 */

.visuallyhidden {
    border: 0;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
}

/*
 * Extends the .visuallyhidden class to allow the element to be focusable
 * when navigated to via the keyboard: h5bp.com/p
 */

.visuallyhidden.focusable:active,
.visuallyhidden.focusable:focus {
    clip: auto;
    height: auto;
    margin: 0;
    overflow: visible;
    position: static;
    width: auto;
}

Comments

10

input may have a hidden attribute:

label{ cursor:pointer; user-select:none; }
input:checked + span::before {
  content: 'un';
}
<label>
  <input type='checkbox' hidden/>
  <span>check</span>
<label>

Comments

0

if you want your check box to keep its height and width but only be invisible:

.hiddenCheckBox{
    visibility: hidden;
}

if you want your check box to be invisible without any with and height:

.hiddenCheckBox{
    display: none;
}

1 Comment

Both ways the checkbox is not focusable, don't use this.
-1

You need to add the element type to the class, otherwise it will not work.

.hide-checkbox { display: none }        /* This will not work! */
input.hide-checkbox { display: none }   /* But this will. */ 
<input class="hide-checkbox" id="checkbox" />
<label for="checkbox">Checkbox</label>

It looks too simple, but try it out!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.