CindyH CindyH - 1 month ago 5
jQuery Question

Is this the correct syntax to check for radio OR checkbox select?

I have a quiz plugin and I've added a div to display specific messages if someone presses the VOTE button before selecting an answer. Once they actually choose an answer I wanted to make the error message div fade back out, so included at the end

jQuery('input[type="radio"]').click(function() {
jQuery('#error-messages').fadeOut();
});


Now I just realized that some of the multiple choice questions use checkboxes instead, so I changed the first line of the code to

jQuery('input[type="checkbox"],input[type="radio "]').click(function() {


It works, but is that the right way to do it?

Answer

Yes, this is the most succinct way of selecting multiple types of input fields. There's no way to make that any simpler unless you start getting into using classes on your form elements.

Comments