-2

I'm honestly stumped. I was working on a program and I narrowed down the problem to an incredible degree; whenever I try to change the innerHTML of an element to "\f" it doesn't display.

Note: I've only tried this on CodePen and JSFiddle; 2 online code editors. I'm not sure if my problem is the websites, or JavaScript. I'd prefer to keep this online as it's the best choice for portability.

Also Note: Just in case anyone was wondering, I need this to use the MathJax Library for JavaScript. (\Frac specifically) https://codepen.io/FoodLover195/pen/mBzoyG

HTML:

<p id="Test">Hello World</p>
<p id="Test2">Hello World</p>
<p id="Test3">Hello World</p>

JavaScript:

document.getElementById("Test").innerHTML = "\f";
document.getElementById("Test2").innerHTML = "\food";
document.getElementById("Test3").innerHTML = "\fried \food";
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Kye Gregory
  • 15
  • 1
  • 1

2 Answers2

2

\f is an escape sequence for a formfeed (U+000C) in a string literal in JavaScript (and most other languages with C-like syntax). To put an actual backslash in a string literal, use \\ (e.g., \\f).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

\ is an escape character. You need to escape the \ character. In other words:

document.getElementById("Test").innerHTML = "\\f";
document.getElementById("Test2").innerHTML = "\\food";
document.getElementById("Test3").innerHTML = "\\fried \\food";
davidchoo12
  • 1,261
  • 15
  • 26