I have this code:
<html>
<body>
<script>
function myFunction (){
// some javascript
}
document.getElementsByClassName('test')[0].setAttribute("onclick", "myFunction()");
</script>
<button class="test"> Submit Request </button>
</body>
</html>
I want to use JavaScript that executes on page load to add an onclick attribute to my button on the page.
Right now in doing this I get an error that says cannot read proprety 'setAttribute' of undefined.
Does anyone know why I am getting this error and how to fix it?
Note: My only option to access the button is via it's class attribute and I cannot physically modify the button element in any way.
Thanks!