Nachshon Schwartz
Nachshon Schwartz

Reputation: 15765

trouble getting .on function to work as it is supposed to

I have an html page with the following html:

<div id="content">
    <div class="pagination">
        <a href=''>blah</a>
    </div>
</div>

Going to a page that has the following js doesn't return any alert:

$("#content").on(".pagination a", "click", function(event){
    alert("alert message")
})

Going to the same page with this js does work:

$("#content .pagination a".on("click", function() {
    alert("alert message")
})

why? in the .on() API the first one is supposed to bind the delegated functionality to the content id and the second is supposed to bind it to the a tag within the content id, yet the first one doesn't happen.

What is wrong with the way I'm using on() function to delegate the click event?

Upvotes: 0

Views: 47

Answers (2)

Adil Shaikh
Adil Shaikh

Reputation: 44740

.on() is different from .delegate()

Correct syntax for .on() is -

on( events [, selector ] [, data ], handler(eventObject) )

You need this :

$("#content").on("click",".pagination a", function(event){
    alert("asdasdasd")
})

Upvotes: 2

TGH
TGH

Reputation: 39258

$("#content").on("click",".pagination a", function(event){
    alert("asdasdasd")
})

Switch to the above definition

Upvotes: 2

Related Questions