Radio Button: get or set checked value

PHOTO EMBED

Wed Feb 07 2024 00:39:20 GMT+0000 (Coordinated Universal Time)

Saved by @ppSan #html #form #radio_button #javascript #jquery

/** Vanilla javascript **/

    // Get the value of the checked radio button, undefined on not checked or not found
   let radioButtonValue = (document.querySelector("input[name='radioName']:checked") || {}).value || undefined

    // Check radio button with value ...
    (document.querySelector("input[name='radioName'][value='A']") || {}).checked=true;

/** jQuery **/

    // Get the value of the checked radio button, undefined on not checked or not found
   let radioButtonValue = $("input[name='radioName']:checked").val();

    // Check radio button with value ...
   $("input[name='radioName'][value='A']").prop('checked',true);
content_copyCOPY