I have two input
s of type radio
. For each input
there's a correspoding label
with a single button
inside.
I was expecting that clicking the button would have the same effect as clicking the label: that the corresponding input would be checked.
However, this does not happen. As shown by the following snippet, hovering and pressing the buttons does trigger the corresponding style changes in the radio buttons, but the click action does not select the input, even though the simple labels work as expected.
I've checked that button
s are legal children of label
s. Labels allow Phrasing Content, and buttons are Phrasing Content, so everything should be okay there.
I have also tried to add an event listener to both buttons' click events, and within them calling event.preventDefault()
, just to make sure that the default behaviour of the button was not preventing the event from bubbling up to the label, but to no avail, the label is receiving the event.
Since this seems to be consistent across browsers (Tested on Firefox 41a and Opera 31b / Chrome 44):
<div>
<input type="radio" name="A" id="one" />
<label for="one">One</label>
<label for="one">
<button type="button">One</button>
</label>
<input type="radio" name="A" id="two" />
<label for="two">Two</label>
<label for="two">
<button type="button">Two</button>
</label>
</div>
Don't place interactive elements such as anchors or buttons inside a label .
Explanation: Label control is used to display a static text on the form, such as title. Command button is used to invoke an action when user clicks on it.
You can style a span inside a label to look like a button. You don't need to add the button itself. The example of a modal on the page you linked to is invalid anyway as you can't put a div inside a label element. If I understand your solution, you are making the label look like a button.
The <label> tag defines the label for <button>, <input>, <meter>, <output>, <progress>, <select>, or <textarea> element. The <label> tag can be used in two ways: Firstly, use <label> tag by providing the <input> and id attribute. The <label> tag needs a for attribute whose value is the same as input id.
A label
can only be associated with one form control at a time. This is evidenced by the fact that the for
attribute points to an element with a matching ID attribute.
You have a button
that is a descendant of your label
; the expected interpretation of this is that the label
serves as a label for the button
. However, you're trying to associate the radio button, not the button
element, with the label
. The real problem here is that there is a conflict between the form controls and the label
; it's unable to figure out which control it's supposed to be associated to.
I'm guessing that the fact the radio button isn't working correctly is a side effect of this. Perhaps it's down to some activation behavior in both the radio button and the button
element.
I've checked that
button
s are legal children oflabel
s. Labels allow Phrasing Content, and buttons are Phrasing Content, so everything should be okay there.
The validator does nevertheless produce the following error with your markup:
Error: Any
button
descendant of alabel
element with afor
attribute must have an ID value that matches thatfor
attribute.
This is because a label
element with a for
attribute needs to have a form control with that ID value for the for
attribute to point to, even if that control is a descendant of the label
itself. But you can't assign the same ID to more than one element. The result is the aforementioned conflict.
Without knowing what you're trying to accomplish here, the best advice I can give if you just want the label
to have the appearance of a button is to just style it as such.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With