Javascript (event)

function( event ) {   
  event.target.style.color = "purple";
}

i don’t understand the event part, what does it target? where does the value pass too?

With only this snippet it does not target anything because there is no event ;).

event is an object that is created upon the execution of an “event” such as onclick, mouseover, hover etc.

The target is the property of this event defined by the element the event was called on.

This means that your function has to be linked to an event. For example:

let element = document.getElementById('button'); // lets do something with a button

element.onclick = function (event) {
   event.target.style.color = "purple";
}

Here we have an element that is a button, when the user clicks on it, it will execute the function, you mentioned.

The function will get the click event as input. The target of this click event is element so the function will then apply a style attribute of color with the value of “purple”.

2 Likes