0

I want my button to trigger a download of a PDF file when clicked. However, I am having trouble getting the button to fire the function when clicked. For now, I am just wanting the button to console.log "it works"

I have selected the correct element, and defined the type as "button", and am able to console.log the button. But when attaching the button.onclick()= function{ console.log("it works");}; it does not trigger the console.log in the console. I have also put the onclick function into a window.onload function.

 <div id="resume" class="resume">
    <button type="button" id="resume-button" class="resume-button">RESUME</button>
</div>

--JavaScript--

var button = document.querySelectorAll('.resume-button');

window.onload = function(){

button.onclick = function(){
    console.log("yay its working");
}; }

I am expecting the console to output "it works" when the button is clicked, but nothing happens when clicked.

Jonathan Soto
  • 21
  • 1
  • 4

1 Answers1

2

document.querySelectorAll returns an NodeList so that you need to access the button as the first element of this NodeList (document.querySelectorAll('.resume-button')[0])

var button = document.querySelectorAll('.resume-button')[0];

button.onclick = function(){
  console.log("yay its working");
};
<div id="resume" class="resume">
    <button type="button" id="resume-button" class="resume-button">RESUME</button>
</div>

Also note there is no need to wrap the onclick function assignment into window.onload

antonku
  • 7,377
  • 2
  • 15
  • 21