Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button inside a label

Tags:

I have a label with "for="the pointer to the checkbox input"" and as long as I know, this for can be added only for label. Therefore, I need to add inside of the label a <button>(I need it), but the click event isn't working properly - it doesn't check the checkbox the for is pointing to.

What are the possibilities I can use here if I must place <button> inside the label, with only html+css coding?

some code for example:

<input type="checkbox" id="thecheckbox" name="thecheckbox"> <div for="thecheckbox"><button type="button">Click Me</button></div> 
like image 573
nikoletta Avatar asked May 28 '16 22:05

nikoletta


People also ask

Can a button be inside an A tag?

The a element may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links). In other words, you can nest any elements inside an <a> except the following: <a> <audio> (if the controls attribute is present)

How do I label a button in HTML?

You can create it using a <label> element. You will need to bind the label to the input element by setting the for attribute on the label to the same value as the id attribute on the input element.

Can a button have a label?

Yes, your button must have so form of text label associated with it.

Can you put input inside label?

No, you can't put anything inside an input tag. You can put an input tag inside a label, and the label will be automatically associated with that input. That means when you click on the label, it focusses the input.


2 Answers

It turns out you can make a <button> operate an input, but only if you put the <label> inside the <button>.

<button type="button">    <label for="my-checkbox">Button go outside the label</label>  </button>  <input type="checkbox" id="my-checkbox">

Although this contravenes the W3C specs:

The interactive element label must not appear as a descendant of the button element. https://www.w3.org/TR/html-markup/label.html

like image 156
Sinetheta Avatar answered Sep 24 '22 14:09

Sinetheta


You can use transparent pseudo element that overlays the checkbox and the button itself that will catch mouse events.

Here's an example:

html:

<label>   <input type="checkbox">   <button class="disable">button</button> </label> 

css:

.disable{pointer-events:fill} label{position:relative} label:after{   position: absolute;   content: "";   width: 100%;   height: 100%;   background: transparent;   top:0;   left:0;   right:0;   bottom:0; } 
like image 20
eboye Avatar answered Sep 24 '22 14:09

eboye