JQuery delegate not working?

I am trying to use a delegate to call a function when I click on a specific set of links. I already used it on another page. He worked there. But this time I could not even find where the problem is.

My links were dynamically generated, they look like

<a class="thumbLink" href="#" id="My HomeTown-1"><small>My HomeTown</small></a>

And the jQuery delegate function,

$(document).ready(function(){
$("#albumHolder").delegate('a.thumbLink', 'click', this.loadAlbum);//albumHolder is div ID that holds all the <a> elements

this.loadAlbum = function(){
    var id = this.id;
    var number = id.lastIndexOf("-");
    var alubmName = id.subString(0, number);
    alert(albumName);
}
});

Update:

I tried both versions, but I can't get it to work!

$("#albumHolder").delegate('a.thumbLink', 'click', function loadAlbum(){
    var id = this.id;
    var number = id.lastIndexOf("-");
    var alubmName = id.subString(0, number);
    alert(albumName);
});



   this.loadAlbum(){
    var id = this.id;
    var number = id.lastIndexOf("-");
    var alubmName = id.subString(0, number);
    alert(albumName);
}
$("#albumHolder").delegate('a.thumbLink', 'click', this.loadAlbum);

The above code is in $(document).ready(function(){});. It's right?? I also tried putting them in a separate file js. I can not make it work.

Any idea about my problem !!!

+3
source share
2 answers

If this is the order of your actual code, you are trying to pass an expression to the function before it is initialized.

, :

this.loadAlbum = function(){
    var id = this.id;
    var number = id.lastIndexOf("-");
    var alubmName = id.subString(0, number);
    alert(albumName);
}
$("#albumHolder").delegate('a.thumbLink', 'click', this.loadAlbum);
+5

. . (function loadAlbum() { /* ... */ }), this.loadAlbum (this.loadAlbum = function() { /* ... */ }), delegate().

+4

Source: https://habr.com/ru/post/1775937/


All Articles