How to check if radio or checkbox is selected in JavaScript & jQuery?

How to check if radio or checkbox is selected in JavaScript & jQuery?

Hello Devs, 

In this short tutorial, you will learn about how to check if radio or checkbox is selected in JavaScript & jQuery? This tutorial goes into details on how to check if radio input or checkbox input is selected or not with JavaScript and we will check it with jQuery also. 

So let's get started.

Radio Input

Let's check for radio input first. How can i check if radio input is selected or not?

Check with JavaScript

const radioBtn = document.getElementById('myRadioBtn');
if (radioBtn.checked) {
  // Radio button is selected
} else {
  // Radio button is not selected
}

Check with jQuery

if ($('#myRadioBtn').is(':checked')) {
  // Radio button is selected
} else {
  // Radio button is not selected
}

 

Checkbox Input

Now let's check for checkbox input. 

Check with JavaScript

const checkbox = document.getElementById('myCheckbox');
if (checkbox.checked) {
  // Checkbox is checked
} else {
  // Checkbox is not checked
}

Check with jQuery

if ($('#myCheckbox').is(':checked')) {
  // Checkbox is checked
} else {
  // Checkbox is not checked
}

 

I hope it will help you. 
Happy Coding :)

Leave a Comment

Your email address will not be published. Required fields are marked *

Go To Top
×