Randomblue
Randomblue

Reputation: 116283

Referring to "this" without using .each()

I have an element $myElement that I want to refer to using this inside .css() call. Specifically, I tried to change the CSS property someCssProperty to $myElement.myProperty naively:

$myElement.css(someCssProperty, this.myProperty);

Is there a way of having a working this without using the cumbersome .each()?

Upvotes: 0

Views: 44

Answers (1)

Dennis
Dennis

Reputation: 32598

css takes a function as a parameter which you can use:

$myElement.css(someCssProperty, function(index, value) {
    return this.myProperty;
});

The callback also receives the index in the set and the old value as parameters.

Upvotes: 3

Related Questions