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>
2

2 Answers 2

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");
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Getting this error: Uncaught RangeError: Maximum call stack size exceeded
0

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

e.preventDefault()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.