0

I'm making a list of options with radio buttons, but these are inside a table, belongs to a TD.

I've tried to highlight the table row with a color when pressing the radio button, but then it highlights all the table rows and not the one I pressed.

Once I press, for instance: Option 1. I only want to highlight Option 1.

Look at this example: look jsFiddle

Pinch
  • 31
  • 11

6 Answers6

2

How about using relative selectors? Additionally, you shouldn't have more than one element with the same id, so I fixed that too.

//jQuery implementation
$('[name=valgt]').click(function() {
    $(".checked").removeClass("checked");
    if($(this).is(':checked')) {
        $(this).closest('.crimeOption').addClass('checked');
    }
});
.checked {
    background: linear-gradient(to right, rgb(76, 140, 128), rgba(55, 119, 120, 1));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<table>
  <thead>
    <tr>
      <th>Head
      </th>
    </tr>
  </thead>
  <tbody class="radio-list">
    <tr class="crimeOption">
      <td>
        <input type="radio" name="valgt" />
        <label>Option 1.
          </td>
    </tr>
    <tr class="crimeOption">
      <td>
        <input type="radio" name="valgt" />
        <label>Option 2.
          </td>
    </tr>
    <tr class="crimeOption">
      <td>
        <input type="radio" name="valgt" />Option 3.
      </td>
    </tr>
    <tr class="crimeOption">
      <td>
        <input type="radio" name="valgt" />Option 4.
      </td>
    </tr>
    <tr class="crimeOption">
      <td>
        <input type="radio" name="valgt" />Option 5.
      </td>
    </tr>
  </tbody>
</table>
Neil
  • 14,063
  • 3
  • 30
  • 51
  • You can use `closest()` function instead of `parents()`. As `closest()` is more efficient than `parents()`. Check [here](http://stackoverflow.com/questions/9193212/difference-between-jquery-parent-parents-and-closest-functions#9193419) for details. – Mahesh Singh Chouhan Apr 05 '17 at 21:04
  • @MaheshSinghChouhan Thanks, I updated the answer. Thanks! – Neil Apr 05 '17 at 21:06
1

Instead of refering to each input separately, you could select all of them, bind change event listener and add the checked class to the parent of actually selected input.

$('.crimeOption input').change(function() {
  $('.crimeOption td').removeClass('checked');
  if ($(this).is(':checked')) {
    $(this).parent().addClass('checked');
  } 
});
.checked {
  background: linear-gradient(to right, rgb(76, 140, 128), rgba(55, 119, 120, 1));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th>Head</th>
    </tr>
  </thead>
  <tbody class="radio-list">
    <tr class="crimeOption">
      <td><input type="radio" name="valgt" id="valgt1" /><label>Option 1.</td>
                    </tr>
                    <tr class="crimeOption">
                        <td><input type="radio" name="valgt" id="valgt2" /><label>Option 2.</td>
                    </tr>
                    <tr class="crimeOption">
                        <td><input type="radio" name="valgt" id="valgt3" />Option 3.</td>
                    </tr>
                    <tr class="crimeOption">
                        <td><input type="radio" name="valgt" id="valgt4" />Option 4.</td>
                    </tr>
                    <tr class="crimeOption">
                        <td><input type="radio" name="valgt" id="valgt5" />Option 5.</td>
                    </tr>
                </tbody>
            </table>
kind user
  • 40,029
  • 7
  • 67
  • 77
0

You're selecting the td's with a class selector that they all share. You'll need to change:

        $('.crimeOption').addClass('checked');

to

        $(this).closest('td').addClass('checked');
Michael Arrison
  • 1,464
  • 2
  • 13
  • 22
0

Updated your jquery function on jsfiddle code here (https://jsfiddle.net/b4w0cnbf/7/)

$('input[type="radio"]').click(function() {
    $('.crimeOption').removeClass('checked');
    if($(this).is(':checked')) {
        $(this).closest('.crimeOption').addClass('checked');
    }
});

Updating jquery code as per comment request, Updated jsfiddle link:

$('input[type="radio"]').click(function(e) {
    $(this).closest('.crimeOption').trigger('click');
});

$('.crimeOption').click(function() {
    $('.crimeOption').removeClass('checked');
    if($(this).find('input[type="radio"]').is(':checked')) {
        $(this).find('input[type="radio"]').prop("checked", false);
        //$(this).addClass('checked');
    } else {
        $(this).find('input[type="radio"]').prop("checked", true);
        $(this).addClass('checked');
    }

});
Mahesh Singh Chouhan
  • 2,558
  • 1
  • 16
  • 26
-1

How about this

$("input[type='radio']").change(function(){
  var value = $( 'input[name=opt]:checked' ).val();
  $("input[type='radio']").parent('td').css("background","initial");
 $(this).parent('td').css("background","red");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
                <thead>
                    <tr>
                        <th>Head</th>
                    </tr>
                </thead>
                <tbody class="radio-list">
                    <tr class="crimeOption">
                        <td><input type="radio" name="valgt" id="valgt1" /><label>Option 1.</td>
                    </tr>
                    <tr class="crimeOption">
                        <td><input type="radio" name="valgt" id="valgt2" /><label>Option 2.</td>
                    </tr>
                    <tr class="crimeOption">
                        <td><input type="radio" name="valgt" id="valgt3" />Option 3.</td>
                    </tr>
                    <tr class="crimeOption">
                        <td><input type="radio" name="valgt" id="valgt4" />Option 4.</td>
                    </tr>
                    <tr class="crimeOption">
                        <td><input type="radio" name="valgt" id="valgt5" />Option 5.</td>
                    </tr>
                </tbody>
            </table>
$("input[type='radio']").change(function(){
  var value = $( 'input[name=opt]:checked' ).val();
  $("input[type='radio']").parent('td').css("background","initial");
    $(this).parent('td').css("background","red");
});
Robert I
  • 1,509
  • 2
  • 11
  • 18
-1

This is simply done by CSS

Just use :checked selector

input[type="radio"]:checked + label{
  background:red;
}
Ajay
  • 196
  • 2
  • 12