Maarten Wolfsen Maarten Wolfsen - 1 month ago 6
Javascript Question

val() function giving console error

The

input[type="number"]
has a
max
attribute, so people can't click higher than the number given. But this doesn't count for typed numbers, they can exceed far beyond the max number given. So, with jQuery, I want to make a solution for this. I already got this:

var productPageInput = $('#product-options-wrapper .option input[type="number"].qty');
var max = 60;
productPageInput.attr('max', max);

productPageInput.bind('keyup input', function(e){
console.log($(this).val());
if( $(this).val() > max ){
$(this).val() = max;
}
});


But, everytime I execute this code, and I type above 60, this console error appears:

Uncaught ReferenceError: Invalid left-hand side in assignment(…)


Which points to this line:

$(this).val() = max;


What is going wrong here?

Answer

To assign a value:

$(this).val(max);

In general, a function call on the left-hand side of an assignment operator in JavaScript doesn't make sense.

Comments