0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

チェックしたらラベルを太字にする

Last updated at Posted at 2020-06-12

チェックボックス編
html側で labelタグを書いとくだけでいい。重宝してる。

html
<label><input name="hogehoge" id="hogehoge" type="checkbox" value="1"></label>
javascript
$(function() {
 $('input[type=checkbox]').filter(":checked").each(function() {
      $(this).closest('label').css('font-weight', 'bold');
    })
    .end().on('change', function() {
      $(this).prop('checked') ? $(this).closest('label').css('font-weight', 'bold') : $(this).closest('label').css('font-weight', 'normal');
  });
});

ラジオボタン編
idとname をhtmlで記載しておくこと。
nameはそのグループで統一させること。

html
<label><input type="radio" value="1" name="hogehoge" id="hogehoge1">hogehoge1</label>
<label><input type="radio" value="2" name="hogehoge" id="hogehoge2">hogehoge2</label>
javascript
$(function() {
  $(":radio").click(function(){
    var ans_name = ( $( this ).attr( "name" ) );
    var ans_id = ( $( this ).attr( "id" ) );
    console.log(ans_name);//デバッグ用
    console.log(ans_id);//デバッグ用
    $('input[name=' + ans_name).closest('label').css('font-weight', 'normal');
  $('#' + ans_id).closest('label').css('font-weight', 'bold');
  });
});
0
1
0

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?