jonmrich jonmrich - 1 year ago 110
jQuery Question

Get value from radios as boolean instead of string

I have a function like below to get the values of a series of radio that can either be True or False.

universalDetails = ['thing1', 'thing2', 'thing3'];
$.each(universalDetails, function(idx, name) {
universalDetails[idx] = {
name: name,
value: $('input[name=' + name + ']:checked', '#universalAttributes').val()
};
});


However, when I run this, the
value
comes back as a string ("True" or "False") versus a boolean. How can I adjust this to ensure I return a boolen and push into
universalDetails
?

Answer Source

You can use .is() to check the current element

value: $('input[name=' + name + ']', '#universalAttributes').is(':checked')

OR Use the length property

value: $('input[name=' + name + ']:checked', '#universalAttributes').length > 0
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download