Should I getElementById on button click or on page load? [closed]

Pretty much what the title states, I am debating without much experience if I should getElementId on click or as a global var when the page loads.

So, it’s either this:

var cp1 = document.getElementById('cp1');
var olp = document.getElementById('olp');

ob.addEventListener("click", func_ob);
function func_ob() {
  cp1.classList.add('hidden');
  olp.classList.remove('hidden');
}

Or this:

ob.addEventListener("click", func_ob);

function func_ob (element1, element2) {
  document.getElementById(element1).add('hidden');
  document.getElementById(element2).remove('hidden');
}

If I had to make a guess, I would say this is loading time vs run time? I’m not sure, I could really use someone more experienced to break it down for me a bit.

Thanks in advance!