1

I have a code where I am selecting radio button using the .prop() method. Issue is I also have the onclick event on radio button is there any workaround to make it trigger an onclick event as well using jQuery

jQuery Code

/**
 * Select shipping method when selected from the list
 */
$(document).ready(function() {
    var $options = $('.shipping-method .item');

    $options.click(function(e) {
        var $current = $(this);
        e.preventDefault()
        e.stopPropagation()
        $options.removeClass('on')
        $current.addClass('on')
        $('input', $current).prop('checked', true)
    })
})

HTML Code

<li>
    <div class="item"><a href="javascript:;" class="shipping_3">Home Delivery</a>
        <input name="shipping" type="radio" class="radio" id="shipping_3" value="3" checked="checked" supportcod="1" insure="0" onclick="selectShipping(this)">
    </div>
</li>
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Axad Doe
  • 11
  • 3
  • Please create a [mcve] in order to help you better. – 31piy Aug 31 '18 at 04:46
  • 2
    Possible duplicate of [Using jQuery to trigger html onclick event](https://stackoverflow.com/questions/4624670/using-jquery-to-trigger-html-onclick-event) – Esko Aug 31 '18 at 04:55

2 Answers2

0

Here you go with a solution

$current.prop('checked', true).trigger("click");

$(document).ready(function() {
  var $options = $('.shipping-method .item');

  $options.click(function(e) {
    var $current = $(this);
    e.preventDefault()
    e.stopPropagation()
    $options.removeClass('on')
    $current.addClass('on')
    $current.prop('checked', true).trigger("click");
  });
});
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
0

My suggestion would be to remove onclick attribute with a separate event listener first.
Refer jQuery.click() vs onClick.
Remove line:

e.preventDefault()
Aditya Sharma
  • 645
  • 1
  • 10
  • 28