0

I'm trying to do a very simple task but for some reason I can't do it.

basically I am using the if statement to change the value of an input field using javascript but it doesn't work!

this is my code:

if (document.getElementById("colour").value == "171515"){
    document.getElementById("colour").value = "db0000";
}

if (document.getElementById("colour").value == "db0000"){
    document.getElementById("colour").value = "171515";
}

and the HTML looks like this:

<input type="text" id="colour" name="colour" class="colour" value="171515"/>

so what i need to do is this:

I launch a page and the input field is on my page with the value of value="171515", and then I press a button and that should change the value of the input field to value="db0000", and then I press the button again, and it should change the value of the input button to value="171515" and I need to do the same steps as many times as I want.

currently, it seems like it gets into a loop action and thats why it doesn't change the value of input field! (correct me if i'm wrong).

any help would be appreciated.

Thanks

EDIT:

The javascript code above is executed like so:

$(params.addPplTrigger).bind('click', function(e){
                    e.preventDefault();

///////// THE CODE ABOBE WILL GO HERE///////////

}
2
  • Possibly button events may be the reason how you're executing it? Commented Dec 1, 2014 at 12:59
  • 3
    It's worth noting that you have two ifs, one changes the value one way, the other changes it back. Use if (condition) {...} else if (condition) {...} or a simple if (condition) {...} else {...}. Commented Dec 1, 2014 at 12:59

2 Answers 2

1

You're just missing an else:

if (document.getElementById("colour").value == "171515"){
    document.getElementById("colour").value = "db0000";
}

else if (document.getElementById("colour").value == "db0000"){
    document.getElementById("colour").value = "171515";
}

What happens in your original code

Case 171515:

  • first if condition evaluates to true, because the value is 171515
  • value gets changed to db0000
  • second if condition evalutes to true again, because the value is db0000 now
  • value gets changed back to 171515

Case db0000:

  • first if condition gets evaluates to false, because the value is not 171515
  • the value remains the same
  • second if condition gets evaluates to true, because the value is db0000
  • value gets changed to 171515

So, in both cases you'd end up with 171515.

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

6 Comments

If there's only two possibilities (and one must be true) the else if (condition) can be replaced by a simple else (though not my down-vote).
WORKED. THANKS EVER SO MUCH. I up VOTED THE ANSWER .... I DON'T KNOW WHY SOMEONE DOWNVOTED IT!!!!
Ha well spotted (My initial comment was far too hasty!)
@DavidThomas well spotted
Yeah, I'm really curious at what happens in the brain of those reflex-downvoters at times... You're welcome, hope I helped you understand what was going on.
|
0

since you are doing :

if (document.getElementById("colour").value == "171515"){
    document.getElementById("colour").value = "db0000";
}

and then reverse

if (document.getElementById("colour").value == "db0000"){
    document.getElementById("colour").value = "171515";
}

so you are not able to see the change .use else instead of second if.

if (document.getElementById("colour").value == "171515"){
    document.getElementById("colour").value = "db0000";
}else{
    document.getElementById("colour").value = "171515";
}

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.