Mike Van Stan
Mike Van Stan

Reputation: 396

Adding an Event Listeners

I was trying to make two EventListeners for the same event.

Until I realized you can't really do that - For example:

thumbnailPageButton.addEventListener("click", onThumbnailPageButton, false);
thumbnailPageButton.addEventListener("click", alertSomething);

So I placed the code I wanted under the "onThumbnailPageButton" and removed the "alertSomething" part.

/* Event handler for click events dispatched by ThumbnailPageButton to update the visibility of UI elements. */
function onThumbnailPageButton(event){
    updateUI();
    alert("Yes you clicked it!");
}

However this seems to break it all. and the thumbnail icons now will not show.

Upvotes: 0

Views: 42

Answers (1)

PeterM
PeterM

Reputation: 441

You can do multiple listeners of the same event type on the same object - I presume that's what you were trying to do. See below.

Your code has a few odd things. You tagged it as JQuery but didn't use JQuery (which would look like $('#thumbnailPageButton').click(function(){etc})). Also, you aren't calling your function alertSomething (nor do you have the function listed in your code). Try this:

var thumbnailPageButton = document.getElementById('thumbnailPageButton');
thumbnailPageButton.addEventListener("click", onThumbnailPageButton, false);
thumbnailPageButton.addEventListener("click", alertSomething);


function onThumbnailPageButton(){
    alert('hey');
}

function alertSomething(){
   alert('here');
}

Fiddle here: https://jsfiddle.net/anwh0epe/

Upvotes: 1

Related Questions