1

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.

3 Answers 3

3

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'; }
Sign up to request clarification or add additional context in comments.

Comments

1

== compare

= assign

if (behind == "nse") 

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.