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?
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?
-
Well, the left-hand side of an assignment obviously has to be a reference. An assignment stores the value from the right-hand side into the reference on the left-hand side. The result of calling
val()
is a string, not a reference, hence the error. – Šime Vidas Commented Jul 31, 2011 at 23:21
3 Answers
Reset to default 7In jQuery you assign a new value with:
jQuery("#audit").val(newval);
val()
without a variable works as a getter, not a setter.
jQuery doesn't plain 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();
}
the right syntax would be:
jQuery("#audit").val(newval);
val is a function, that, when executed, can't be assigned a value like you try to do.