This post will discuss how to generate an HTML button element in JavaScript and jQuery.

The HTML button element represents a clickable button that is often used to submit forms and implement standard button functionality anywhere on the document.

 
In vanilla JavaScript, you can use the document.createElement() method to programmatically create an HTML button element and set its required attributes. Then to append the button to a container, you can use the Node.appendChild() method.

This method is demonstrated below:

JS


HTML


CSS



Edit in JSFiddle

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

JS


HTML


CSS



Edit in JSFiddle

 
Here’s a shorter version you can use:

JS


HTML


CSS



Edit in JSFiddle

 
Using .appendTo() method:

JS


HTML


CSS



Edit in JSFiddle

That’s all about creating dynamic HTML button elements with JavaScript and Query.