(function ($) {
function changeRadioButton(element, value) {var name = $(element).attr("name");$("[type=radio][name=" + name + "]:checked").removeAttr("checked");$("[type=radio][name=" + name + "][value=" + value + "]").attr("checked", "checked");$("[type=radio][name=" + name + "]:checked").change();}
function getRadioButton(element) {var name = $(element).attr("name");return $("[type=radio][name=" + name + "]:checked").attr("value");}
var originalVal = $.fn.val;$.fn.val = function(value) {
//is it a radio button? treat it differently.if($(this).is("[type=radio]")) {
if (typeof value != 'undefined') {
//setterchangeRadioButton(this, value);return $(this);
} else {
//getterreturn getRadioButton(this);
}
} else {
//it wasn't a radio button - let's call the default val function.if (typeof value != 'undefined') {return originalVal.call(this, value);} else {return originalVal.call(this);}
}};})(jQuery);
$(function() {// this outright does not work properly as explained above$('#reported label').click(function() {var query = $('input[name="filter"]:checked').val();var time = (new Date()).toString();
$('.query[data-method="click event"]').html(query + ' at ' + time);});
// this works, but fails to update when same label is clicked consecutively$('#reported input[name="filter"]').on('change', function() {var query = $('input[name="filter"]:checked').val();var time = (new Date()).toString();
$('.query[data-method="change event"]').html(query + ' at ' + time);});
// here is the solution I came up with$('#reported label').click(function() {var query = $('#' + $(this).attr('for')).val();var time = (new Date()).toString();
$('.query[data-method="click event with this"]').html(query + ' at ' + time);});});
$(function () {// Someone has clicked one of the radio buttonsvar myform= 'form.myform';$(myform).click(function () {var radValue= "";$(this).find('input[type=radio]:checked').each(function () {radValue= $(this).val();});})});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class="my-test"><form>Group 1<fieldset><label><input type="radio" name="example-1" value="Foo" required />Foo</label><label><input type="radio" name="example-1" value="Bar" required />Bar</label></fieldset>Group 2<fieldset><label><input type="radio" name="example-2" value="Banana" required />Banana</label><label><input type="radio" name="example-2" value="Apple" required />Apple</label></fieldset><button type="submit">Submit</button></form><p>Press this button to just get the value of the first group: <button class="radio-button" id="example-1">Foo or Bar?</button></p><p>Press this button to just get the value of the second group: <button class="radio-button" id="example-2">Banana or Apple?</button></p></div>
$(document).ready(function() {$('input[name="radioName"]').on('change', function() {const val = $(this).filter(":checked").val();alert(val);})
// First load checkconst val = $('input[name="radioName"]').filter(":checked").val();alert(val);});