views:

42

answers:

2

Quick question, I have a simple javascript problem that I don't know the fix to.

divNext = document.getElementById("submit_button");
if(document.getElementById("page").value == 4 ? divNext.value = 'Submit' : divNext.value = 'Next »');

Now the problem is that it should display the Next with two >> after it (at least if they where encoded) however it displays them with the actual content there. How can I get it to display the html encoded character? Thanks in advanced

+3  A: 

That is because you have HTML-encoded the character instead of encoding it to be in a Javascript string.

And by the way, you are using both the if and the ? conditional operator incorrectly.

divNext = document.getElementById("submit_button");
divNext.value = document.getElementById("page").value == 4 ? 'Submit' : 'Next \xBB';

Or using if:

divNext = document.getElementById("submit_button");
if (document.getElementById("page").value == 4) {
  divNext.value = 'Submit'
} else {
  divNext.value = 'Next \xBB';
}
Guffa
@Pointy: Or an `input` element with `type="button"` or `type="submit"` which is more likely.
Guffa
yes sorry about that!
Pointy
So when doing this in JavaScript I need JavaScript "Encoded" characters I take it. Thanks =). Accidentally forgot to take off the if when converted it from using the if statement to the ternary operator.
Craig
A: 

Just put them in

document.getElementById("page").value == 4 ? divNext.value = 'Submit' : divNext.value = 'Next >>');

or use .innerHTML

document.getElementById("page").value == 4 ? divNext.innerHTML = 'Submit' : divNext.innerHTML = 'Next »»');

Also, if you are using the ternary operator why do you have an if?

qw3n
Good question - that statement is messed up.
Pointy
@qw3n: You are using the conditional (ternary) operator is if it was a statement, not an operator.
Guffa