I am using the following to create buttons at runtime:
var dynButton = $('<input/>').attr({ type: 'button', id:'idDynBtn', class:'clsDynBtn', name:'DynBtn', value:'Tool' });
How to place an icon or image on the button created as above?
<input type="button"> can't be an image. <input type="image" src="..."> can show an image, and <button><img src="..."></button> can show an image. See Input Type image submit form value?
type=submit is a button.
here is the example https://jsfiddle.net/t7gvd10c/
var dynButton = $('<input/>').attr({ type: 'submit', id:'idDynBtn', class:'clsDynBtn', name:'DynBtn', value:'Tool' });
I did it with font awesome and jquery same as follow:
function addButton(txt,icon){
let btn = document.createElement("button");
$(btn).html(`<i class="${icon}"></i> ${txt}`);
$(btn).appendTo($("#btnContainer"));
}
addButton("home","fa fa-home");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div id="btnContainer">
</div>
I hope it useful for you.