0

I've got a jQuery function to modify the width values of two CSS classes. The width is given in percentage for both classes. As written, it works, but I'm trying to optimize my jQuery code so it works with just variables. Here is the relevant code (percent is an earlier defined variable):

$('.eventdescription').css({width : 75 - percent + '%'});
$('.function').css({width : 25 + percent + '%'});

I'm trying to get rid of the '75' and '25' values. I've tried the following:

$('.eventdescription').css({width : -=percent + '%'});
$('.function').css({width : +=percent + '%'});

But I get an 'unexpected identifier' error. Enclosing '+=percent' and '-=percent' in quotes doesn't seem to do anything. What am I doing wrong? Thanks for reading.

EDIT: See Tom Chung's answer. The correct syntax is enclosing the += and -= operators in quotes and including them as discrete units in the operation.

1 Answer 1

1
$('.eventdescription').css({width : '-=' + percent + '%'});
$('.function').css({width : '+=' + percent + '%'});

'-=' && '+=' operator should be in form of string.

Further explaining, the code +=percent is invalid. Proper usage of it should be,

var originalPercent = 0;
originalPercent += percent;
// Now, value of originalPercent will be originalPercent + percent
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.