Required radio button does not validate in form, when clicked through Javascript/jQuery

I have a radio button that selects a dealer in my checkout. I need this button to automatically be checked for the user - however, no matter what I do, the radio button still needs to be clicked to validate.

Here's what I've tried so far:

document.getElementById("#dealer-selection-" + elm.id).click();

This makes the radio button show up as if it's been clicked, however when submitting form, a notice still pops up with a validation error.

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

Same thing as above - this shows the radio button as clicked, but for the purposes of the submit button, it is not.

jQuery("#dealer-selection-" + elm.id).trigger("click",function(){
                selectDealer(jQuery(this).val());
            });

Again, this shows the button as clicked, but it is not.

I have even tried simply adding checked="CHECKED" to the radio button itself, which also shows as checked, but does not validate.

Thanks in advance

Topic radio validation jquery forms Wordpress javascript

Category Web


I finally found one that works for me:

document.getElementById("#dealer-selection-" + elm.id).checked=true;

For some reason, this one checks the radio button visually, while also checking it as an option.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.