setattribute onclick code example
Example 1: set onclick javascript
<button id="myBtn">Try it</button>
<p id="demo"></p>
<script>
document.getElementById("myBtn").onclick=function(){displayDate()};
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
Example 2: javascript setattribute onclick function with parameters
var but = document.createElement('button');
var my_string = '123-45-lol , @%# ';
var but.setAttribute('onclick', 'my_function( " '+my_string+' " )');
// Or to use addEventListener() instead of setAttribute() ?