Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 210 characters in body
Source Link
Powerlord
  • 89k
  • 17
  • 130
  • 179

With jQuery, it'd be something like

if ($('input[name=gender]:checked').length > 0) { // do something here } 

Let me break that down into pieces to cover it more clearly. jQuery processes things from left to right.

input[name=gender]:checked 
  1. inputinput limits it to input tags.
  2. [name=gender][name=gender] limits it to tags with the name gender within the previous group.
  3. :checked:checked limits it to checkboxes/radio buttons that are selected within the previous group.

If you want to avoid this altogether, mark one of the radio buttons as selectedchecked (checked="checked") in the HTML code, which would guarantee that one radio button is always selected.

With jQuery, it'd be something like

if ($('input[name=gender]:checked').length > 0) { // do something here } 

Let me break that down into pieces to cover it more clearly. jQuery processes things from left to right.

input[name=gender]:checked 
  1. input limits it to input tags.
  2. [name=gender] limits it to tags with the name gender within the previous group.
  3. :checked limits it to checkboxes/radio buttons that are selected within the previous group.

If you want to avoid this altogether, mark one of the radio buttons as selected in the HTML code, which would guarantee that one radio button is always selected.

With jQuery, it'd be something like

if ($('input[name=gender]:checked').length > 0) { // do something here } 

Let me break that down into pieces to cover it more clearly. jQuery processes things from left to right.

input[name=gender]:checked 
  1. input limits it to input tags.
  2. [name=gender] limits it to tags with the name gender within the previous group.
  3. :checked limits it to checkboxes/radio buttons that are selected within the previous group.

If you want to avoid this altogether, mark one of the radio buttons as checked (checked="checked") in the HTML code, which would guarantee that one radio button is always selected.

Source Link
Powerlord
  • 89k
  • 17
  • 130
  • 179

With jQuery, it'd be something like

if ($('input[name=gender]:checked').length > 0) { // do something here } 

Let me break that down into pieces to cover it more clearly. jQuery processes things from left to right.

input[name=gender]:checked 
  1. input limits it to input tags.
  2. [name=gender] limits it to tags with the name gender within the previous group.
  3. :checked limits it to checkboxes/radio buttons that are selected within the previous group.

If you want to avoid this altogether, mark one of the radio buttons as selected in the HTML code, which would guarantee that one radio button is always selected.