JavaScript / jquery: add class if a radio button is selected and data attribute qualifies

user3291025

I have a page with a multiple choice question. If the user selects the correct answer, I would like the explanation under their choice to turn from grey to green. I have only been able to get all the choices to turn green, no matter what is selected, and can't seem to get this right. Help is much appreciated, thanks.

html

<li class='test-questions'>
  <input data-correct="true" id="answer_id_1" name="answer_id" type="radio" value="1" />
   A) I am the correct answer!
  <div class='question_explanation'>
    <p>Correct. This is an explanation of the correct answer.</p>
  </div>
</li>

<li class='test-questions'>
  <input data-correct="false" id="answer_id_2" name="answer_id" type="radio" value="2" />
   B) I am an incorrect answer.
  <div class='question_explanation'>
    <p>Incorrect. This is an explanation why the answer is incorrect.</p>
  </div>
</li>

css

div.question_explanation {
background-color: LightGray;
}

div.correct_answer_green {
background-color: LightGreen;
}

bad javascript/jquery function

$("input[name*='answer_id']").each(function() {
  if ($(this).data('correct') === true && $(this).is(':checked')) {
  $('div.question_explanation').addClass('correct_answer_green');
  }
});

the same bad function in coffeescript if you prefer

$("input[name*='answer_id']").each ->
  if $(this).data('correct') == true and $(this).is(':checked') 
    $('div.question_explanation').addClass 'correct_answer_green'
  return
Satpal

Use

//Bind change event
$("input[name*='answer_id']").change(function() {
    //If condition
    if ($(this).data('correct') === true && $(this).is(':checked')) {
        $(this)
            .closest('li') //Find parent li
            .find('div.question_explanation') //find your div
            .addClass('correct_answer_green');
    }
}).trigger('change'); //Trigger on page load

$(document).ready(function() {
  $("input[name*='answer_id']").change(function() {
    if ($(this).data('correct') === true && $(this).is(':checked')) {
      $(this).closest('li').find('div.question_explanation').addClass('correct_answer_green');
    }
  }).trigger('change');
});
div.question_explanation {
  background-color: LightGray;
}
div.correct_answer_green {
  background-color: LightGreen;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li class='test-questions'>
    <input data-correct="true" id="answer_id_1" name="answer_id" type="radio" value="1" />A) I am the correct answer!
    <div class='question_explanation'>
      <p>Correct. This is an explanation of the correct answer.</p>
    </div>
  </li>

  <li class='test-questions'>
    <input data-correct="false" id="answer_id_2" name="answer_id" type="radio" value="2" />B) I am an incorrect answer.
    <div class='question_explanation'>
      <p>Incorrect. This is an explanation why the answer is incorrect.</p>
    </div>
  </li>
</ul>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Add class to selected radio button

From Dev

Add class to selected radio button

From Dev

add class for selected radio button in thymeleaf

From Dev

Add jquery when radio button is selected

From Dev

Get data attribute from radio button with jQuery

From Dev

How do I add class to label when radio button is selected?

From Dev

Set the class on selected radio button

From Dev

Add selected Attribute jquery

From Dev

jQuery Check if a Radio Button is Selected or not

From Dev

JavaScript - Checking if Radio Button is selected

From Dev

How to: If data attribute equals radio select add class

From Dev

Remove Div Attribute when radio button is selected

From Dev

If radio button is selected and select value is selected jquery

From Dev

jQuery add class based on paren data attribute

From Dev

jQuery: Add class if list item has checked radio button inside

From Dev

Trying to use javascript to add unique class when radio button checked

From Dev

radio button of a class clicked jquery

From Dev

jQuery if radio button is selected then proceed to next element

From Dev

How to know which radio button is selected in jquery?

From Dev

jQuery - Run function/code if radio button is NOT selected

From Dev

jquery radio button selected still showing error

From Dev

Validating if the radio button group is selected JQUERY

From Dev

Is there a way to check if the nth radio button is selected in jQuery?

From Dev

prob with selected radio button using javascript?

From Dev

JavaScript validation to check if a radio button is selected?

From Dev

Get selected radio button value in javascript

From Dev

How to get the value of a selected radio button and checkbox using its class in jQuery?

From Dev

check if at least one radio button is selected from inputs with the same class javascript

From Dev

jQuery selected radio button index among different sets of radio buttons

Related Related

  1. 1

    Add class to selected radio button

  2. 2

    Add class to selected radio button

  3. 3

    add class for selected radio button in thymeleaf

  4. 4

    Add jquery when radio button is selected

  5. 5

    Get data attribute from radio button with jQuery

  6. 6

    How do I add class to label when radio button is selected?

  7. 7

    Set the class on selected radio button

  8. 8

    Add selected Attribute jquery

  9. 9

    jQuery Check if a Radio Button is Selected or not

  10. 10

    JavaScript - Checking if Radio Button is selected

  11. 11

    How to: If data attribute equals radio select add class

  12. 12

    Remove Div Attribute when radio button is selected

  13. 13

    If radio button is selected and select value is selected jquery

  14. 14

    jQuery add class based on paren data attribute

  15. 15

    jQuery: Add class if list item has checked radio button inside

  16. 16

    Trying to use javascript to add unique class when radio button checked

  17. 17

    radio button of a class clicked jquery

  18. 18

    jQuery if radio button is selected then proceed to next element

  19. 19

    How to know which radio button is selected in jquery?

  20. 20

    jQuery - Run function/code if radio button is NOT selected

  21. 21

    jquery radio button selected still showing error

  22. 22

    Validating if the radio button group is selected JQUERY

  23. 23

    Is there a way to check if the nth radio button is selected in jQuery?

  24. 24

    prob with selected radio button using javascript?

  25. 25

    JavaScript validation to check if a radio button is selected?

  26. 26

    Get selected radio button value in javascript

  27. 27

    How to get the value of a selected radio button and checkbox using its class in jQuery?

  28. 28

    check if at least one radio button is selected from inputs with the same class javascript

  29. 29

    jQuery selected radio button index among different sets of radio buttons

HotTag

Archive