Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery/Javascript Invalid left-hand side in assignment

I am using this relatively simple code:

var height = help ? 'minus' : 'plus';
var prop = $('#properties');

if(height == 'minus'){
    prop.height(prop.height() -= 206);
} else {
    prop.height(prop.height() += 206);
}

It fails on both lines that do the adding/subtracting! Any ideas?

like image 434
benhowdle89 Avatar asked Mar 21 '12 11:03

benhowdle89


People also ask

How do I fix invalid left-hand side in assignment?

The "Invalid left-hand side in assignment" error occurs when we have a syntax error in our JavaScript code. The most common cause is using a single equal sign instead of double or triple equals in a conditional statement. To solve this, make sure to correct any syntax errors in your code.

Which of these JavaScript assignment operator is invalid?

A single “=” sign instead of “==” or “===” is an Invalid assignment.

What is assignment error in JS?

The JavaScript exception "invalid assignment to const" occurs when it was attempted to alter a constant value. JavaScript const declarations can't be re-assigned or redeclared.


2 Answers

The -= operator equals operand = operand - value which in your case would look like

prop.height() = prop.height() - 206;

which obviously will fail. You just need the minus operator to accomplish that task.

prop.height(prop.height() - 206);

will do it.

like image 100
jAndy Avatar answered Oct 26 '22 22:10

jAndy


you can't -= a method.

either you need to prop.height(prop.height() - 206); or collect the value first and then -= it like...

var h = prop.height();
h -= 206
 prop.height( h);
like image 32
Yevgeny Simkin Avatar answered Oct 27 '22 00:10

Yevgeny Simkin