Domanda

How do I get the ID associated with the this keyword? I've tried several things from SO pages but here is where I've decided to open the question:

$('.anySelector').waypoint(function() {
    console.log(this); // entire selector's content
    var thisContent = this;
    console.log($(thisContent).attr('id')); // undefined
});
È stato utile?

Soluzione

If this really is a DOM Element (which is very probable using jQuery) it is as simple as just accessing the id property of the element.

jQuery( '.anySelector' ).waypoint( function() {
    console.log( this.id );
} );

If you really need to go the jQuery way (i.e. using the attr method) you must turn this into a jQuery object first:

jQuery( '.anySelector' ).waypoint( function() {
    console.log( jQuery( this ).attr( 'id' ) );
} );
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top