This post will discuss how to bind the change event handler to the radio button in JavaScript and jQuery.

1. Using jQuery

The idea is to bind the change event handler to the radio button using the .change(handler) method. Now, an alert would be displayed whenever a radio button is selected.

JS


HTML



Edit in JSFiddle

 
The .change(handler) method is a shortcut for .on("change", handler). Therefore, the following code is the same as above:

2. Using JavaScript

In vanilla JavaScript, you can use the onchange property to specify an event handler to receive change events. Now, the onchange event handler is fired whenever any change is made to the radio button.

Edit in JSFiddle

 
Note that it is bad practice to mix JavaScript with HTML markup. Alternatively, you can select the radio group using the document.querySelectorAll() and handle the change event with EventTarget.addEventListener() method.

Edit in JSFiddle

That’s all about binding change event handler to radio button with JavaScript and Query.