JQuery script gives incorrect values for radio button fields

By: Jane Onorati | Asked: 01/15/2025
ForumsCategory: General questionsJQuery script gives incorrect values for radio button fields
Jane OnoratiJane Onorati asked 3 hours ago

I have a script in the "After Fields" section of the settings of a form.  It's a form that is used by a registered user to edit their profile.  I need to get some values from the form when a user opens it to edit their profile.  Here is some code I am testing just to see if I can retrieve the user's entry values:


jQuery(document).ready(function ($) {
var parent = $("input[name='item_meta[622]']").val();
console.log("Value of parent/guardian field is " + parent);
var mail_list = $("input[name='item_meta[614]']").val();
console.log("Value of mailing list field is " + mail_list);
var user_acct = $("input[name='item_meta[921]']").val();
console.log("Value of user account field is " + user_acct);
var type = $("#field_sk5w3").val();
console.log("Value of type field is " + type);
});

All radio button fields are yes/no fields. The value for the first two fields in my test entry is "No" and the third one is "Yes". But they all are reported by the script as having "Yes" values. The text field reports the correct text value. What am I doing wrong?

1 Answers
Victor Font answered 3 hours ago
You omitted the ":checked" attribute. Radio buttons are a group.
Jane OnoratiJane Onorati replied 2 hours ago

Thanks Victor! I had tried that before but must have had the syntax wrong or it was a caching issue or something. This works: var parent = $("input[name='item_meta[622]']:checked").val();

Say, is there a way to use keys for this instead of IDs? I know how to do it in PHP but not JQuery/JS.

Victor Font replied 2 hours ago

Yes, you can use $("[id^=field_mykey]:checked").val()

Jane OnoratiJane Onorati replied 43 minutes ago

Excellent!!

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