Any ideas? This code was working and stopped. Can’t figure out why.

By: Walter Jones | Asked: 08/30/2022
ForumsCategory: General questionsAny ideas? This code was working and stopped. Can’t figure out why.
Walter JonesWalter Jones asked 2 years ago
So I recently put some code in after fields on this form to prevent more than 1 check box from being selected.  It was working great, and I have changed nothing but the code doesn’t seem to work now… Any thoughts? <script>

jQuery(document).ready(function($){
var limit = 1;// Change 3 to your limit
$('input[name="item_meta[322][]"]').change(function(){
var checkbox_num = $("input[name='item_meta[322][]']:checked").length;
if ( checkbox_num > limit ) {
this.checked = false;
}
});
});
</script> The form loads here https://yourebooked.com/mbc/book-session/    
Rob LeVineRob LeVine replied 2 years ago

Hey Walter - is there a reason you can't use the "Limit Selections" option under the Checkboxes Options?

Walter JonesWalter Jones replied 2 years ago

Ah yeah I forgot to mention this is a lookup field displayed as a checkbox. So that option isn’t available.

1 Answers
Best Answer
Walter JonesWalter Jones answered 2 years ago
Nervermind easy fix. Changed to radio buttons.  
Rob LeVineRob LeVine replied 2 years ago

Ha! I was going to ask, "Isn't a checkbox that's limited to one choice just a radio button?"

Walter JonesWalter Jones replied 2 years ago

But now I need to figure out how to auto-select the radio button on page load for a particular radio button. Cant seem to find any good working jquery on google.

Rob LeVineRob LeVine replied 2 years ago

I'm usually pretty good at that stuff. On that page, if I issue $("#field_1f2ra-0").prop('checked', true); in the debugger, it selects the "Fall Swing" choice. Is that what you want to do?

Walter JonesWalter Jones replied 2 years ago

Hey Rob. If you click fall swing it brings up a date. Then you click the date it brings up a time. Then I have a 4 digit radio that’s shows up that I call the time key. I want it to be auto selected as it’s the only option. I plan on hiding that radio in production if I can get it to be pre selected.

Rob LeVineRob LeVine replied 2 years ago

Maybe I'm telling you something you already know, however, what I'd do for both the date and time is to get all the radio children from its container, count them, and if there's only one, select it.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right