This post will discuss how to dynamically generate an <input type=button> in JavaScript and jQuery.

1. Using JavaScript

In plain JavaScript, you can use the document.createElement() method to create an HTML <input> elements of type button and set its required attributes. Then to append the input element to a container, you can use the Node.appendChild() method. This approach is demonstrated below:

JS


HTML



Edit in JSFiddle

2. Using jQuery

If you work with jQuery, you can use the .append() method to append a button at the end of the specified container. Here’s a working example:

JS


HTML



Edit in JSFiddle

 
The code can be shortened to:

JS


HTML



Edit in JSFiddle

 
Here’s alternate version using .appendTo() method:

JS


HTML



Edit in JSFiddle

 
Please note that the latest <button> element is now recommended over the <input> elements of type button to create buttons.

That’s all about dynamically generate an input type=button in JavaScript and jQuery.