Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does jQuery complain about an "invalid left-hand assignment"?

function auditUpdate(newval) {
    jQuery("#audit").val() = newval;
    jQuery("#auditForm").submit();
}

Why do I get an error where I try to assign newval to the #audit value?

like image 737
Frayce Avatar asked Jul 31 '11 23:07

Frayce


2 Answers

In jQuery you assign a new value with:

jQuery("#audit").val(newval);

val() without a variable works as a getter, not a setter.

like image 101
Niklas Avatar answered Sep 28 '22 07:09

Niklas


jQuery doesn't complain about it. But your JavaScript interpreter does. The line

jQuery("#audit").val() = newval;

is invalid JavaScript syntax. You can't assign a value to the result of a function call. Your code says "call val, get the return value -- and then assign newval to the return value." This makes no sense.

Instead:

function auditUpdate(newval) {
    jQuery("#audit").val(newval);
    jQuery("#auditForm").submit();
}
like image 43
T.J. Crowder Avatar answered Sep 28 '22 08:09

T.J. Crowder