cdw100100
cdw100100

Reputation: 1

jquery calculation need assistance.!

The problem is that when you hit the equals sign on the calculator it says Uncaught ReferenceError: Invalid left-hand side in assignment. Can someone explain to me what im doing wrong

$("#Num_View").val($("#Num_View").val() = eval($("#Num_View").val()));

Upvotes: 0

Views: 34

Answers (2)

Jason P
Jason P

Reputation: 27012

You are trying to assign a value to the result of a function that returns a string, which isn't valid.

I think what you're going for is this:

$("#Num_View").val($("#Num_View").val() + ' = ' + eval($("#Num_View").val()));

Which would convert user input of 2 + 3 to 2 + 3 = 5.

Or better:

$("#Num_View").val(function(_, oldValue) {
    return oldValue + ' = ' + eval(oldValue);
});

However, eval()ing user input is definitely a bad idea.

Upvotes: 0

isick
isick

Reputation: 1487

You are setting the return of the val() function (which is a string literal) equal to something else. This can't be done.

$("#Num_View").val() = eval($("#Num_View").val()

That part is throwing the error. If you want to compare those values you need to use double equals == OR if you want to set the value you need to pass it as a parameter to the val function like

$("#Num_View").val(eval($("#Num_View").val())

Upvotes: 1

Related Questions