-2

I would like to write an empty character. I'm learning javascript on w3School and stumbled upon this

<!DOCTYPE html>
<html>
<body>

<h2>What Can JavaScript Do?</h2>

<p id="demo">JavaScript can change HTML content.</p>

<button type="button" onclick='document.getElementById("demo").innerHTML = "Hello JavaScript!"'>Click Me!</button>

</body>
</html>

It's said that simple and double quote can be used, with little trickery i did this:

<!DOCTYPE html>
<html>
<body>

<h2>What Can JavaScript Do?</h2>

<p id="demo">JavaScript can change HTML content.</p>

<button type="button" onclick="document.getElementById(&quot;demo&quot;).innerHTML = &quot; &amp; quot; is &amp;quot; &quot;">Click Me!</button>

</body>
</html>

Which display after clicking the button: & quot; is " But the html special character for " is &quot; and not & quot;

On the other hand, if I write this: &amp;quot; instead of &amp; quot; I get " But if I take away the semicolon: &ampquot; I do get &quot;

I'm using chrome and I tried it on firefox, nonetheless, I suspect this behaviour to be unsafe and just be an extrapolation from the browser.

So,Is there a way to write an empty character in HTML? That would allow me to write &amp;&EMPTYHTMLCODE;quot; to get the desired &quot;

I've looked up: http://www.degraeve.com/reference/specialcharacters.php but didn't find anything.

NOTE: writing straight "&quot;" without separating &quot and; with ** (bold tags) will interpret "&quot;" as """

Sheed
  • 577
  • 4
  • 18
  • 2
    https://stackoverflow.com/questions/26972529/is-there-a-html-character-that-is-blank-including-no-whitespace-on-all-browser – Pitto May 23 '18 at 11:43
  • 1
    avoid using w3schools - it's got some bad practices and just things that are no longer valid/correct. Refer to MDN. Also avoid using inline js/css - it's bad practice that leads to hard to maintain code – treyBake May 23 '18 at 11:43
  • You can use `­` --> &­quot; or you can use `innerText` instead `innerHTML`. – Kavian K. May 23 '18 at 12:25

2 Answers2

1

Use innerText instead innerHTML:

<p id="demo">JavaScript can change HTML content.</p>

<button type="button" onclick="document.getElementById(&quot;demo&quot;).innerText = &quot; &amp;quot; is \&quot; &quot;">Click Me!</button>
Kavian K.
  • 1,340
  • 1
  • 9
  • 11
  • The point was using quotes insides quotes, this doesn't solve the problem, `"&quot; is ""` won't work at all. I will still have to, first use &quote; to delimit then go by the solution you formerly suggested using ­ or ​ code (as shown in pitto's comment post) – Sheed May 23 '18 at 13:00
1

You can escape quotes with slash (\") to use them inside the same type of quotes in JavaScript:

<h2>What Can JavaScript Do?</h2>

<p id="demo">JavaScript can change HTML content.</p>

<button type="button" onclick='document.getElementById("demo").innerHTML = "Hello nested \"\" in JavaScript!"'>Click Me!</button>

The &something; syntax is HTML special character escaping, not JavaScript. In JavaScript strings, it works only when you use it for HTML output, and it is (sometimes) tricky because the HTML parsing rules that apply to .innerHTML() method are somewhat tricky.

Ilya Streltsyn
  • 13,076
  • 2
  • 37
  • 57
  • oddly i tried to do so, but by replacing the simple quote by double quote, i couldn't escape " character i had to use html entities but, which I didn't know, i can eventually escape it by writing *\"* instead of *\"* – Sheed May 23 '18 at 15:41