check radio button is checked or not in jquery by name

Given a group of radio buttons:

<input type="radio" id="radio1" name="radioGroup" value="1">
<input type="radio" id="radio2" name="radioGroup" value="2">

You can test whether a specific one is checked using jQuery as follows:

if ($("#radio1").prop("checked")) {
   // do something
}

// OR
if ($("#radio1").is(":checked")) {
   // do something
}

// OR if you don't have ids set you can go by group name and value
// (basically you need a selector that lets you specify the particular input)
if ($("input[name='radioGroup'][value='1']").prop("checked"))

You can get the value of the currently checked one in the group as follows:

$("input[name='radioGroup']:checked").val()

Change price according to option select in opencart 3 (in product twig file)

 {% if option.type == 'radio' %}
            <div class="form-group{% if option.required %} required {% endif %}">
              <label class="control-label">{{ option.name }}</label>
              <div id="input-option{{ option.product_option_id }}"> {% for option_value in option.product_option_value %}
                <div class="btn-group" data-toggle="buttons">
      <label class="btn btn-primary">
                    <input onchange="CalcOption(this,this.name)" id="{{ option_value.price }}" type="radio" name="option[{{ option.product_option_id }}]" value="{{ option_value.product_option_value_id }}" />
                    {% if option_value.image %} <img src="{{ option_value.image }}" alt="{{ option_value.name }} {% if option_value.price %} {{ option_value.price_prefix }} {{ option_value.price }} {% endif %}" class="img-thumbnail" /> {% endif %}                  
                    {{ option_value.name }}
                   <!-- {% if option_value.price %}
                    ({{ option_value.price_prefix }}{{ option_value.price }})
                    {% endif %} --></label>
                </div>
                {% endfor %} </div>
            </div>
            {% endif %}
 <script>
var orgprice=$(".fprice").html().substring(1).replace(",", "");
function CalcOption(ctrl,ctrlname)
  {
 var symb=$(".fprice").html().substring(0,1);
$("input[type=radio]").parent('label').removeClass("active");
   var value=0;
   $("input[type=radio]:checked").each(function() {
       value += parseFloat($(this).attr("id").substring(1).replace(",", ""));
  if(!$(this).parent('label').hasClass("active")){
  $(this).parent('label').addClass("active");
  }
   });
  //if(!$(ctrl).parent('label').hasClass("active")){
  //$(ctrl).parent('label').addClass("active");
  //}
   value=parseFloat(orgprice)+parseFloat(value);
 $(".fprice").html(symb+" "+value.toFixed(2));
  }
</script>


Leave a Reply