user2182397
user2182397

Reputation:

How to add EventListener to an html link?

EventListener is not added and test code is not executed when instead of standard html button <input type="button"></input> I refer to <a></a> in the renderer.js:

//code here is executed
...
window.user_button = document.querySelector('#lbUserButton')
user_button.addEventListener('Click', ()=>{
    //code here is not executed
    event.preventDefault();
    ipc.send('event-name');        
})

HTML:

<a id="lbUserButton" href="#" class="someCssClass">
    button title
</a>

Upvotes: 1

Views: 2538

Answers (2)

Douglas
Douglas

Reputation: 36

The event is not executed because you've added an event listener for "Click" while it should be lowercase "click". The code below will work.

user_button.addEventListener('click', () => {
...
})

Upvotes: 2

pevhv
pevhv

Reputation: 68

try this:

var user_button = document.queryselector('#lbUserButton')


I don't know if you have already defined user_button, maybe you forgot. If you are defining it, why put window. before it? I don't think that is necessary. At least, I never put it.

Upvotes: 0

Related Questions