2

Should be a really simple answer but I can't figure it out right now. I have this button:

<button id="logout" type="button">Logout</button>

And it's supposed to run this jQuery code within script tags at the bottom of the body:

$("#logout").addEventListener("click", function () {
    alert('Button Clicked');
});

However, no alert pops up. I don't get it. Thanks in advance.

8 Answers 8

11

addEventListener is a method of DOM element not of jQuery object which is an array-like structure that contains all the selected DOM elements

To attach event using jQuery, use .on => Attach an event handler function for one or more events to the selected elements

Try this:

$("#logout").on("click", function() {
  alert('Button Clicked');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button id="logout" type="button">Logout</button>

Using JS:

document.getElementById("logout").addEventListener("click", function() {
  alert('Button Clicked');
});
<button id="logout" type="button">Logout</button>

Edit: You can get first DOM element from array-like object returned by jQuery selector using $(SELECTOR)[0] or $(SELECTOR).get(0)

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

Comments

1

addEventListener() method registers the specified listener on the EventTarget.

If you really want to use addEventListener then write the following code:

var el = document.getElementById("logout");
el.addEventListener("click", function () {
     alert('Button Clicked');
}, false);

Otherwise do it with jquery

$(document).on("click", "#logout", function () {
    alert('Button Clicked');
});

OR

$("#logout").on("click", function () {
        alert('Button Clicked');
    });

Comments

1

Alternate answer is:

$(document).on("click", "#logout", function() {
  alert('Button Clicked');
});

This works on dynamically created elements as well.

Comments

0

You should use on method to use the event handlers in jquery. The addEventListener is a core JavaScript event method.

$("#logout").on("click", function() {
  alert('Button Clicked');
});

You may still use addEventListner forcing jQuery code to JavaScript:

$("#logout")[0] //Now, this is JavaScript Object
   .addEventListener("click", function () {
    alert('Button Clicked');
});

Comments

0

You can use jQuery click/on function for getting the button to work.

Example:

$("#logout").on("click", function () { alert('Button Clicked'); });
$("#logout").click(function () { alert('Button Clicked'); });

Comments

0
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>

    $(document).ready(function () {
        $("#logout").on("click", function () {
            alert('Button Clicked');
        });
    });
</script>
</head>

<body>
    <button id="logout" type="button">Logout</button>
</body>

</html>

Comments

0

If you go to your console like in firebug and type $() you will see the jQuery object that shows the methods and properties of the jquery function/object. There is no addEventListener on this object.

addEventlistener is a method for the DOM. If you want to take advantage of jQuery by "writing less and do more" use jQuery methods like on. This will allow you to add an event handler on an element.

$(document).on("click", "#logout", function() {
  alert('button clicked');
});

Comments

0
var element = document.getElementById("logout");

element.addEventListener("click", myFunction);

function myFunction() {

     // your code logic comes here
}

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.