I have a piece of code that normally looks like this

jQuery("#dealer-selection-" + elm.id).on("click",function(){

Basically what it does is select a radio button that is required for a checkout form. I want this radio button to be activated from the start, ready to submit.
I have tried the following:

jQuery('.forhandlerRadio:first').attr('checked', 'checked');

and I've also tried putting in
Into the radio button itself. Both of these things graphically check the radio button, as in they fill in the black dot, however when submitting the form it asks for the radio button to be clicked still.

I deleted the previously mentioned code to test if that was what checked if it had been clicked

jQuery("#dealer-selection-" + elm.id).on("click",function(){

This was correct, as without that code, the form cannot be completed, even if clicking the radio button. That's why I tried making it auto clicked using:

jQuery("#dealer-selection-" + elm.id).trigger("click",function(){

This is, just like the other two things I've tried, able to graphically click the radio button for me, but it still does not count as clicked. Any help is appreciated! The code is from a custom retailer plugin, and the checkout form is the basic woocommerce one.

  • can you add the validation code of the form? also it happens the same if you actually click the option?
    – David Lee
    Feb 17, 2017 at 13:54
  • Here is the selectDealer function, if that's what you mean. Basically this is what's being called on.("click"), and it validates just fine: function selectDealer(id){ jQuery("#dealer-id").val(id); } Feb 20, 2017 at 7:39
  • @DavidLee Also, if I actually click, with the original code, it works and checks correctly. And as I mentioned, if I remove the on.("click") code, it doesn't. So I'm sure that's the code that does it, I just can't understand why I can't make it work automatically, even with .trigger Feb 20, 2017 at 7:45

1 Answer 1

// Check .forhandlerRadio:first jQuery 1.6+
jQuery( ".forhandlerRadio:first-child" ).prop( "checked", true );

// Check .forhandlerRadio:first jQuery 1.5.x and below
jQuery('.forhandlerRadio:first-child').attr('checked', true);
  • This checks the field just as the other solutions I have tried. It shows as checked, but it does not validate as checked until I click it. Feb 20, 2017 at 7:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.