How To Use This.id In Function
Instead of assigning variable for this.id, does anyone know the syntax of how would I pass this.id into findIndex function? listing.slideUp(500, function() { var listing_id = t
Solution 1:
If you have to use a browser that does not support arrow functions (=>
) then an alternative is to bind the this
to the method. Ref. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
var object = {
id: 'c'
};
console.log(
$('div').filter(
function(index, element){
return element.id == this.id;
}.bind(object)
).get()
);
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="b"></div><divid="c"></div><divid="d"></div>
Otherwise you can just us an arrow function, which does not change the context of the this
. Ref. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
var array = ['a', 'c', 'e'];
$('div').on('click', function(){
array.forEach(id => {
if (id == this.id) {
console.log(this);
}
});
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="b">b</div><divid="c">c</div><divid="d">d</div>
Post a Comment for "How To Use This.id In Function"