jsuissa
jsuissa

Reputation: 1762

+= operator within .css method in jQuery

Is there any way to achieve a += operator within the css operator (assuming ui.value in the example has a numerical value)

Something like this(which doesn't work):

$("#resume_holder").contents().find('body').children().css('font-size', +=ui.value);

Upvotes: 1

Views: 860

Answers (5)

Andrew Whitaker
Andrew Whitaker

Reputation: 126052

Try:

$("#resume_holder")
    .contents()
    .find('body')
    .children()
    .css('font-size', '+=' + ui.value);

(if you're using jQuery >= 1.6)

according to the docs for css:

As of jQuery 1.6, .css() accepts relative values similar to .animate(). Relative values are a string starting with += or -= to increment or decrement the current value. For example, if an element's padding-left was 10px, .css( "padding-left", "+=15" ) would result in a total padding-left of 25px.

Example: http://jsfiddle.net/JMRPf/ (trivial I know, but proof that it works)

Upvotes: 5

ShankarSangoli
ShankarSangoli

Reputation: 69915

Not sure about css but you can try to implement it using animate method.

$("#resume_holder").contents().find('body').children()
.animate({ fontSize: '+='+ui.value });

Upvotes: 0

gabitzish
gabitzish

Reputation: 9691

You could keep the actual value of font-size into a variable, add the ui.value, and asign it back:

var crtFontSize = parseInt($("#resume_holder").contents().find('body').children().css('font-size'));
crtFontSize += ui.value;
$("#resume_holder").contents().find('body').children().css('font-size', crtFontSize + 'px');

Upvotes: 1

Keith
Keith

Reputation: 5381

Short answer, no.

However this can be accomplished as follows (untested):

$("#resume_holder").contents().find('body').children().each(function() {
    $(this).css('font-size', $(this).css('font-size') + ui.value);
});

Upvotes: 0

Deleteman
Deleteman

Reputation: 8700

You could use the animate method of jquery, but that would display the result gradually.. is that a problem?

$("#resume_holder").contents().find('body').children().animate('font-size', ui.value);

That would change the current font size, to what ui.value contains.

Upvotes: 0

Related Questions