-4

I've searched this here, and I found a few answers but none of them worked for me.

As told in the title, I want to apply a function to a variable.

This is what I have:

var someVar = $('.class');

function runFunction() {
    someVar.css( ... styles here ... );
}

someVar.runFunction();

It only works if I replace 'someVar.runFunction();' with 'runFunction();'.

Your help will be appreciated :)

4 Answers4

3

If you want to set it on jQuery element, you need to use:

$.fn.runFunction = function() {
    return this.css( ... styles here ... );
}

--DEMO--

A. Wolff
  • 74,033
  • 9
  • 94
  • 155
1

Try this

$.fn.greenify = function() {
    this.css( "color", "green" );
};

$( "a" ).greenify(); 
siraj pathan
  • 1,455
  • 1
  • 14
  • 31
0

You might be looking for bind().

Check this similar discussion: Add jQuery function to specific elements

Community
  • 1
  • 1
Ted
  • 3,985
  • 1
  • 20
  • 33
0

I think what you want is to use the function with a parameter, try something like this:

var someVar = $('.class');

function runFunction( element ) {
    element.css( ... styles here ... );
}

runFunction( someVar );
Mottie
  • 84,355
  • 30
  • 126
  • 241