Recently there was a question here in 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 deselected, keeping the selection unique. Responses were given using JavaScript basically by altering the natural behavior of checkbox
by treating the events in that element. I always believed that doing so was not recommended and that the ideal would be to use the radio
element itself for semantic reasons. I came to comment on the possible solution using the appearance
property of CSS, as it maintains the semantics of the code, changing only the appearance of the element, without needing 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 approach using JavaScript is commonly discussed in forums, but few the places that discuss whether it is in fact appropriate to do so.
The conclusion I get is that you should not change the behavior of one element while semantically it is appropriate to use another one, but I can not state canonically what the impact of both the performance, if any, and the usability of the page is. >
Then:
- Is it allowed to use JavaScript to change the natural behavior of one 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 the usability is directly affected, can it be corrected using the
role="checkbox"
attribute?
Note : The example given between
checkbox
andradio
was more for contextualizing the problem and the answers may preferably cover other examples, if necessary.