I'm not used to working with this
and trying to make some simple functions pass it back and forth. I'm not quite sure what javascript is expecting, but I don't think I'm doing it right.
$(".search-result").each(function() {
var x = $(this).find('.past-positions ol').children()
console.log(x)
//this prints as expected
pastJobs(this)
// this does not
})
function pastJobs() {
var x = $(this).find('.past-positions ol').children()
console.log(x)
// this prints as undefined
}
I assume its possible to pass this
to functions, but I don't think I'm doing it in the right way.
What am I doing wrong?
Try pastJobs.call(this)
instead.
Actually, here pastJobs(this)
you're passing the lexical context this
as param rather than binding that context to the function.
You can use the function bind
to achieve what you want:
pastJobs.bind(this)()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With