LoginSignup
0
0

More than 3 years have passed since last update.

ラジオボタンにラベルを紐づける

Last updated at Posted at 2020-08-12

ラジオボタンをデフォルトのものではなく独自でデザインしたものにしたいときは、before,afterで疑似的にラジオボタンを作成します。
(参考:https://shu-sait.com/input-label-id-for/#outline__1)

その際もとのラジオボタンの代わりになるlabelにしっかりとradioのinputタグを紐づけなければボタンとしては機能しません。

example.html
<input id="radio_button" type="radio">
<label for="radio_button">ラベルテキスト</label>

inputタグをlabelタグで囲むやり方でも良いようです。

example2.html
<label>ラベルテキスト
  <input type="radio">
</label>

なぜこのような基本的なことに詰まったかというと、あるコードを改修する形でこの問題に面したのですが、その元のコードではこの紐づけをしなくても動いていたのです。

調べたところ読み込んでいるjsファイルにこれらを紐づけている記述がありました。

html+cssでできることは極力jsではやらないようにしよう(私の上司より)。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0