47
Recently there was a question here on Stack Overflow about changing the default behavior of checkbox
to act on the page as a radio
, that is, when selecting an item, the others should be unchecked, keeping the selection unique. The answers were given using Javascript basically changing the natural behavior of checkbox
treating the events in this element. I always believed that doing so was not recommended and that the ideal would be to use the element itself radio
for semantic reasons. I came to comment on the possible solution using the property appearance
CSS, because it keeps the code semantics, changing only the appearance of the element, without requiring Javascript:
input[type=radio] {
appearance: checkbox;
-moz-appearance: checkbox;
-webkit-appearance: checkbox;
}
<input name="foo" type="radio" value="1" checked> 1
<input name="foo" type="radio" value="2"> 2
<input name="foo" type="radio" value="3"> 3
However, searching, I saw that such a property was removed from CSS 3, so even though browsers still support such a property, I believe its use should be avoided and that the Javascript approach is commonly discussed in forums, but there are few places that discuss whether it is in fact appropriate to do so.
The conclusion I come to is that one should not change the behavior of one element while semantically it is appropriate to use another, but I cannot canonically state what is the impact of both performance, if any, as in the usability of the page.
So:
- It is allowed to use Javascript to change the natural behavior of an element to act as another?
- What is the impact of this approach? Is there a performance difference in page rendering? And how can this affect usability?
- If usability is directly affected, it is possible to correct using the attribute
role="checkbox"
?
Note: the example given between
checkbox
andradio
was more for contextualization of the problem and the answers can preferably address other examples if necessary.
Whether you believe the Bible or not, it contains a story about what happens when people don’t understand each other about basic concepts. It is the fall of the Tower of Babel. Everyone spoke the same language, until the angels came down and made the people of the time unable to agree on the most basic things. So... when your boss, with the clinical symptoms of hydrocephalus, asks you to do a
checkbox
behave with aradio button
, he is doing just that, babelizing the world. Teach him that there is a common visual language in the world of the Web.– Oralista de Sistemas
apperance: checkbox
may not be recommended, but nothing prevents styling an element with others Features of css3. Taking advantage, I edited the reply there. A radio is a radio =)– Renan Gomes
There’s the link to this other question that you quote?
– bfavaretto
It is linked in the comment of Renan already.
– Woss