This post will discuss how to create a dynamic radio button using JavaScript and jQuery.

1. Using jQuery

With jQuery, you can set radio button attributes using the .prop() method and .append() method to append the radio button at the end of a container.

The following example creates a radio button, its label, and optionally a <br> tag.

JS


HTML



Edit in JSFiddle

 
You can extend the solution to create multiple radio buttons in one go:

JS


HTML



Edit in JSFiddle

 
Alternatively, here’s a shorter version:

JS


HTML



Edit in JSFiddle

2. Using JavaScript

To create a radio button programmatically in plain JavaScript, you can use the document.createElement() method. The following example creates a new radio button with necessary attributes and appends it to a container using the Node.appendChild() method.

JS


HTML



Edit in JSFiddle

That’s all about creating a radio button dynamically in JavaScript and jQuery.