jQuery > Get radio button value

by KelseyW

HTML

<div id="YSR_Page:DS_YSR_Mock:mainBlock" class="bPageBlock brandSecondaryBrd apexDefaultPageBlock secondaryPalette">
  <div class="pbHeader">
    <table cellspacing="0" cellpadding="0" border="0">
      <tbody>
        <tr>
          <div class="pbBody">
            <span id="YSR_Page:DS_YSR_Mock:mainBlock:j_id27"></span>
            <div class="striped-question" id="questionRows">
              <div class="form-group">
                <label class="col-md-12 col-lg-5 control-label" id="questionLabel">
                  <span class="nmbr">1.</span>Acts too young for his/her age&nbsp;&nbsp;
                </label>
                <div class="col-md-12 col-lg-7" id="fieldset1">
                  <span class="nmbr"></span>
                  <fieldset style="border: none;">
                    <table role="presentation" id="YSR_Page:DS_YSR:mainBlock:qContent:0:radioSelections" class="radio-list radio-list-inline">
                      <tbody>
                        <tr>
                          <td>
                            <input name="YSR_Page:DS_YSR:mainBlock:qContent:0:radioSelections" id="YSR_Page:DS_YSR:mainBlock:qContent:0:radioSelections:0" value="0" type="radio">
                            <label for="YSR_Page:DS_YSR:mainBlock:qContent:0:radioSelections:0">
                              0 = Not True
                            </label>
                          </td>
                          <td>
                            <input name="YSR_Page:DS_YSR:mainBlock:qContent:0:radioSelections" id="YSR_Page:DS_YSR:mainBlock:qContent:0:radioSelections:1" value="1" type="radio">
                            <label for="YSR_Page:DS_YSR:mainBlock:qContent:0:radioSelections:1">
                              Somewhat or Sometimes True
                            </label>
                          </td>
                          <td>
                            <input name="YSR_Page:DS_YSR:mainBlock:qContent:0:radioSelections"...

JavaScript

$(function() {
  /*focus*/
  /*  $document.ready(function getQ() {
      var theQ = document.getElementsByName('{!$Component.DS_YSR_Mock.mainBlock.qContent}');
        for (var i = 0; i < qContent.length; i++){
      theQ.focus();
      }
      alert(theQ+' is focused');
    })*/

  document.addEventListener('keydown', (event) => {
    const keyName = event.key;
    alert('You pressed ' + keyName);
    if (keyName == '0' || keyName == '1' || keyName == '2') {
      $("input[name='" + name + "][value='" + keyName + "']").prop('checked', true);
    }
  })

  $('input[type=radio]').change(function() {
    var value = $(this).filter(':checked').val();
    /*var getName = $(this).attr('name');*/
    /*var selection=$(this).val();*/
    /*YSR_Page:DS_YSR:mainBlock:qContent:*/
    var qNum = (parseInt($(this).attr('name').substring(40, 41)) + 1) || 1;
    alert('Question ' + qNum + ' = ' + value);
  });

});


/*function setRadio(keyName, value) {
    if (keyName == '0' || keyName == '1' || keyName == '2') {}
    $("input[name='" + name + "][value='" + keyName + "']").prop('checked', true);
    return false;
  }
}*/

/*
   if pressed 0, 1, or 2{
   	set the value for the question
YSR_Page:DS_YSR:mainBlock:qContent:$qNum:radioSelections:$keyName.attr('checked', true);
    check radiobutton with corresponding value
   }
  */

/*

  function radioval(){
  var theQuestion = document.getElementsByName('{!$Component.DS_YSR_Mock.mainBlock.qContent}');
  for (var i = 0; i < theQuestion.length; i++){
  if (theQuestion[i].checked) {
  alert('Selected value: ' + theQuestion[i].value);
  }
  }
  }*/
/*

        $(document).ready(function () {
            $('#divContainer input:radio').change(function () {
                var selectedVal = $("#divContainer input:radio:checked").val();
                alert(selectedVal);
                  //note the change refers to the radio being unchecked so the following will give the deselected radio
                  //why you'd want it, I...