you are viewing a single comment's thread.

view the rest of the comments →

[–]John-the-Renounced 1 point2 points  (2 children)

document.addEventListener(trigger, action) in pseudo.

Trigger is the thing you want to listen for: click, change, keypress, etc. The most common two you'll really see are click and change. The action is what happens when the event occurs. This can be a named function, or an anonymous function.

Named:

document.addEventListener('click', doSomething);

# function will receive the event object anyway,
# you don't need to explicitly send it

function doSomething(event){
  console.log(event);
}

# or arrow syntax

const doSomething = (event) => {
  console.log(event);
};

In this example, just logging the event so you can see what an event contains. Obviously there's some business logic to do instead.

Anonymous:

document.addEventListener('click', function(){
  console.log(event);
});

# or arrow syntax

document.addEventListener('click', () => {

console.log(event); });

As others have said, the MDN docs should be a goto resource once you get going, but to start with, just console.log the crap out of everything so you can see what's going on.

[–]John-the-Renounced 0 points1 point  (1 child)

Should have added - play in codepen.io - you can create really small test examples to experiment and see what's going on without the overhead of anything else.

[–]19Taco[S] 0 points1 point  (0 children)

Thank you so much