0

I'm working on an Electron-based application, and I don't have much experience with it or JavaScript or Node.js. Currently, I just want to close a window by a click on a button.

close.addEventListener('click', function () {
    ipc.send('close-main-window')
})

This totally works! I am just confused with why it works. From what I understand, the first argument in addEventListener is just any arbitrary string. However, I don't specifically write anything to handle a 'click'. This should mean it's built in functionality, I think. Is this part of JavaScript, Node.js, or Electron? And where in the documentation can I find a list of built in events?

2 Answers 2

2

JavaScript has the function addEventListener which adds an event listener (surprise, surprise) to an element. The element in which the listener is applied to now listens for an event, a string passed into the function (in this case click). Once the event is triggered (in this case when a user clicks on the element), it will execute the callback, which is the function you declared. So, consider this:

element.addEventListener("click", function() {
    console.log("hello!");
});

This will log hello every time element is clicked.

You can read more at the Mozilla's Documentation. Here's a list of all the available events.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Are these events just strings? And if so, isn't that sort of bad design? Wouldn't it be preferable for them to have an enum or a list of properties?
The string is passed into a constructor to create a new Event object, var event = new Event(string_from_addEventListener);, so the string is necessary to create an event
0

The first argument is string which represent the event type.

I think internally it works like this

var event = new Event('click');

where Event is an event object & click is already a predefined event of javascript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.