Create a radio button dynamically with JavaScript/jQuery
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$(document).ready(function() { $('#submit').click(function() { $('#container').append( $('<input>').prop({ type: 'radio', id: 'email', name: 'contact', value: 'Email' }) ).append( $('<label>').prop({ for: 'email' }).html('Email') ).append( $('<br>') ); }) }); |
HTML
|
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html> <body> <div id="container"></div> <div> <button id="submit">Create radio button</button> </div> </body> </html> |
You can extend the solution to create multiple radio buttons in one go:
JS
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$(document).ready(function() { $('#submit').click(function() { var radios = ['Email', 'Phone', 'Mail']; for (var value of radios) { $('#container').append( $('<input>').prop({ type: 'radio', id: value, name: 'contact', value: value }) ).append( $('<label>').prop({ for: value }).html(value) ) } }) }); |
HTML
|
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html> <body> <div id="container"></div> <div> <button id="submit">Create radio buttons</button> </div> </body> </html> |
Alternatively, here’s a shorter version:
JS
|
1 2 3 4 5 6 7 8 9 10 11 |
$(document).ready(function() { $('#submit').click(function() { var radios = ['Email', 'Phone', 'Mail']; for (var value of radios) { $('#container') .append(`<input type="radio" id="${value}" name="contact" value="${value}">`) .append(`<label for="${value}">${value}</label></div>`) .append(`<br>`); } }) }); |
HTML
|
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html> <body> <div id="container"></div> <div> <button id="submit">Create radio button</button> </div> </body> </html> |
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
document.getElementById('submit').onclick = function() { var radiobox = document.createElement('input'); radiobox.type = 'radio'; radiobox.id = 'contact'; radiobox.value = 'email'; var label = document.createElement('label') label.htmlFor = 'contact'; var description = document.createTextNode('Email'); label.appendChild(description); var newline = document.createElement('br'); var container = document.getElementById('container'); container.appendChild(radiobox); container.appendChild(label); container.appendChild(newline); } |
HTML
|
1 2 3 4 5 6 7 8 9 |
<!doctype html> <html> <body> <div id="container"></div> <div> <button id="submit">Create radio button</button> </div> </body> </html> |
That’s all about creating a radio button dynamically in JavaScript and jQuery.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)