0

I'm trying to display a variable with this code:

HTML:

<div id="MyEdit">Money</div>

JS:

var price1 = 0;
var current_value=document.getElementById("MyEdit").innerHTML;

if (current_value == "msc" or current_value == "phd") {
    price1 = 150;
}
else {
    price1 = 250;
}

document.getElementById("MyEdit").innerHTML = price1;

Any tips as to what I am doing wrong?

3
  • 3
    How is this different from your last question? Commented Feb 10, 2014 at 21:22
  • 1
    It's a var now, and the guy who answered told me to open a new one if it did not do what I needed. :P Commented Feb 10, 2014 at 21:23
  • is the var price1 in global scope? does window.price1 contain the value when you access it from the console? Commented Feb 10, 2014 at 21:26

1 Answer 1

5

Your if is wrong. There is no operator or. In javascript operator or is declared with ||

You can read more for logical operators here.

Try

var price1 = 0;

if ((current_value == "msc") || (current_value == "phd")) {
    price1 = 150;
}
else {
    price1 = 250;
}

Also i suppose that above this block of code you initialize somewhere current_value

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

price1 should be initialized even with the broken if
@cschuff price1 is initialized to 0. The OP dont gine the declaration of current_value which i suppose is somewhere above with the rest of the code
well, you are right. if setting the innerHTML is executed on the same 'thread' it will break. an uninitialized 'current_value' will not break anything though.

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.