This is how you can pass an argument to a call back that takes one.
const div = document.getElementById('first');
function makeRed(element) {
element.style.backgroundColor = 'red';
}
function addStyleToElement(element, callback) {
callback(element);
}
addStyleToElement(div, makeRed); //makeRed(div)
An example using addEventListener
:
const nameInput = document.getElementById('name');
const messageTextArea = document.getElementById('message');
const focusHandler = event => {
event.target.className = 'highlight';
};
const blurHandler = event => {
event.target.className = '';
};
nameInput.addEventListener('focus', focusHandler);
nameInput.addEventListener('blur', blurHandler);
messageTextArea.addEventListener('focus', focusHandler);
messageTextArea.addEventListener('blur', blurHandler);
Remember that the addEventListener
will pass in the event
object with the associated target element for the event.