Why is jQuery complaining about "invalid left hand assignment"?

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

Why is there an error when I try to set newval to #audit ?

+4
source share
3 answers

In jQuery, you assign a new value with:

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

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

+8
source

jQuery does not complain about this. But your JavaScript interpreter does this. Line

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

JavaScript syntax is not valid. You cannot assign a value to the result of a function call. Your code says: "call val , get the return value, and then assign newval return value." It does not make sense.

Instead

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

correct syntax:

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

val is a function that, when executed, cannot be assigned a value, as you are trying to do.

+1
source

All Articles