Welcome to DU! The truly grassroots left-of-center political community where regular people, not algorithms, drive the discussions and set the standards. Join the community: Create a free account Support DU (and get rid of ads!): Become a Star Member Latest Breaking News Editorials & Other Articles General Discussion The DU Lounge All Forums Issue Forums Culture Forums Alliance Forums Region Forums Support Forums Help & Search

DaveJ

(5,023 posts)
Fri Feb 24, 2012, 08:27 PM Feb 2012

How can I pass 'this' to a Javascript/JQuery function

Is passing 'this' in JavaScript any different than passing any other variable?

It seems to keep appearing undefined in my code.

Basically, my function reacts to a button click...

$(".vote-up-off&quot .click(function () {
howmuch = 1;
initVote(this);
// do other stuff...
}

And I created this function in order to reduce my code...

function initVote(thisObj) {
alert(thisObj); // keeps poping up as undefined
// do stuff...
}

It is incredibly hard to search on the "this" keyword. Google keeps ignoring "this", much less &quot this)" in a search.

1 replies = new reply since forum marked as read
Highlight: NoneDon't highlight anything 5 newestHighlight 5 most recent replies
How can I pass 'this' to a Javascript/JQuery function (Original Post) DaveJ Feb 2012 OP
It's the dollar sign... DaveJ Feb 2012 #1

DaveJ

(5,023 posts)
1. It's the dollar sign...
Fri Feb 24, 2012, 08:44 PM
Feb 2012

I realized it should be $(thisObj) not thisObj. How could I miss something so obvious to everyone on the planet!

Latest Discussions»Retired Forums»Website, DB, & Software Developers»How can I pass 'this' to ...