LeeG
LeeG

Reputation: 105

JQuery If Statement Returns same number regardless of the input

I have a really easy HTML form and some JQuery to calculate a figure.

The issue I have is that the if statement returns the same number irrelevant of the input:

    $(document).ready(function()
    {
        $('input').change(function()
        {
            var behind = escape($('#behind').val());
            var speed = parseInt($('#speed').val());
            var diff;

if (behind = "nse") { diff = '0.83'; }
if (behind = "0.5") { diff = '1.66'; }
if (behind = "0.75") { diff = '2.49'; }

var hsr = speed-diff;
$('#hsr').html(hsr);

        });
    });

If I input nse into the box the calculation uses diff = '2.49' instead of diff = '0.83' as I've programmed.

The jsFiddle page with the full code is at http://jsfiddle.net/Jucna/1/

Please could someone explain what is wrong with the code?

Thanks in advance.

Upvotes: 1

Views: 57

Answers (3)

Mark Walters
Mark Walters

Reputation: 12390

Missing a = in your if statements

if (behind == "nse") { diff = '0.83'; }
if (behind == "0.5") { diff = '1.66'; }
if (behind == "0.75") { diff = '2.49'; }

Using one = assigns a value rather than comparing it.

Upvotes: 1

sasi
sasi

Reputation: 4318

== compare

= assign

if (behind == "nse") 

Upvotes: 1

Adil
Adil

Reputation: 148150

Use comparison operator == instead of assignment operator = in if statement.

Live Demo

if (behind == "nse") { diff = '0.83'; }
if (behind == "0.5") { diff = '1.66'; }
if (behind == "0.75") { diff = '2.49'; }

Upvotes: 3

Related Questions