Menu
Is there a replacement for .innerHTML() that does not strip the HTML codes when displayed?
If I use .createElement() followed by elem.createTextNode() or elem.textContent=
that includes a unicode character, only the actual code is displayed, not the desired character.
``<i>
</i><!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<title>untitled document</title>
<!--<link rel="stylesheet" href="screen.css" media="screen">-->
<style media="screen">
body {
background-color: #f0f0f0;
font: normal 1em / 150% sans-serif;
}
#container {
text-align:center;
}
#container span {
font-size: 2em;
vertical-align: middle;
}
h1 {
font-size: 1.5em;
color: #444;
text-transform: capitalize;
text-align: center;
}
</style>
</head>
<body>
<h1>test</h1>
<div id="container">
This is a vulgar fraction -
</div>
<script>
( function( d ) {
'use strict';
var el = d.createElement('span');
el.appendChild(d.createTextNode(' u00be'));
d.getElementById('container').appendChild(el)
} ( document ) );
</script>
</body>
</html><i>
</i>
``
<!DOCTYPE html><html lang="en"><head><title> Unicode Test Page </title><br/>
<meta charset="UTF-8"><br/>
<meta name="viewport" content="width-device-width,initial-scale=1.0, user-scalable=yes"/><br/>
<!-- From: <URL url="https://stackoverflow.com/questions/61615640/inserting-value-with-label-using-innerhtml#61615640">https://stackoverflow.com/questions/61615640/inserting-value-with-label-using-innerhtml#61615640</URL> -->
<style><br/>
.sbs { float: left; }<br/>
.sbs > pre,<br/>
.sbs > div { margin: 2em; }
.tile { width: 9em; border: 1px solid red; font-size: 1.75em; }<br/>
.dPRE {white-space: pre-wrap; }<br/>
</style><br/>
</head><body><br/>
<a href="<URL url="https://unicode-table.com/en/sets/arrow-symbols/">https://unicode-table.com/en/sets/arrow-symbols/</URL>" target="_blank">Unicodes</a><br/>
<hr><br/>
<div class="sbs"><br/>
<h1>&lt;pre&gt; elements</h1><br/>
<pre class="tile dPRE" id="p0"></pre><br/>
<pre class="tile dPRE" id="p1"></pre><br/>
<pre class="tile dPRE" id="p2"></pre><br/>
<pre class="tile dPRE" id="p3"></pre><br/>
</div>
<div class="sbs"><br/>
<h1>&lt;div&gt; elements</h1><br/>
<div class="tile" id="d0"></div><br/>
<div class="tile" id="d1"></div><br/>
<div class="tile" id="d2"></div><br/>
<div class="tile" id="d3"></div><br/>
</div>
<script type="text/javascript"><br/>
console.clear();
(function (doc) { <br/>
var pointStart = "12", pointEnd = "40", d = document;
// different symbols for testing<br/>
// var s='u2660', c='u2663', h='u2665', d='u2666';<br/>
// var s='u002b', c='u2212', h='u00d7', d='u00f7';<br/>
var s='u2295', c='u2296', h='u2297', d='u2298';
function preCode( ) {<br/>
doc.getElementById("p0").innerHTML<br/>
= <C>
Friends invited u2192 ${pointStart} / u21d2 ${pointEnd} RSVP ${s}</C>; // poor (uses innerHTML)<br/>
doc.getElementById("p1").textContent<br/>
= <C>
Friends invited → ${pointStart} / ⇒ ${pointEnd} RSVP ੧</C>; // NFG (Not Fully Good)
<i> </i><CODE>doc.getElementById("p2").textContent
<i> </i> = 'Friends invited u2192 '+pointStart+' / u21d2 '+pointEnd+' RSVP '+h; // OK (mixed)
<i> </i>doc.getElementById("p3").textContent
<i> </i> =
Friends invited u2192 ${pointStart} / u21d2 ${pointEnd} RSVP ${d}; // best (templates)</CODE>
}
function divCode( ) {<br/>
doc.getElementById("d0").innerHTML<br/>
= <C>
Friends invited u2192 ${pointStart} / u21d2 ${pointEnd} RSVP ${s}</C>; // poor<br/>
doc.getElementById("d1").textContent<br/>
= <C>
Friends invited → ${pointStart} / ⇒ ${pointEnd} RSVP ੧</C>; // NFG
<i> </i><CODE>doc.getElementById("d2").textContent
<i> </i> = 'Friends invited u2192 '+pointStart+' / u21d2 '+pointEnd+' RSVP '+h; // OK
<i> </i>doc.getElementById("d3").textContent
<i> </i> =
Friends invited u2192 ${pointStart} / u21d2 ${pointEnd} RSVP ${d}; // best</CODE>
}
// TEST SECTION<br/>
preCode();<br/>
divCode();<br/>
})(document);<br/>
</script>
</body></html><br/>
innerHTML
`</C> or <C>
` textContent
`</C> :D
<CODE>
`<i>
</i><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">
<title> Unicode Test Page</title>
<style media="screen">
.form-group pre {
width: 14em;
white-space: pre-wrap;
border: 1px solid #f00;
font-size: 1.75em;
}
.form-group ~ div {
width: 14em;
border: 1px solid #000;
font-family: monospace;
font-size: 1.75em;
}
</style>
</head>
<body>
<h1> test</h1>
<a href="https://unicode-table.com/en/sets/arrow-symbols/">Unicodes</a>
<h2>innerHTML and textContent</h2>
<div class="form-group">
<pre id="points0"></pre>
<pre id="points1"></pre>
<pre id="points2"></pre>
<pre id="points3"></pre>
</div>
<h2>Element.createTextNode</h2>
<div id="foo"></div>
<script>
'use strict';
( function( d ) {
var pointStart = 12,
pointEnd = 40,
s = 'u2295', c = 'u2296',
h =' u2297', e = 'u2298';
d.getElementById("points0").innerHTML =
Friends invited u2192 ${pointStart} / u21d2 ${pointEnd} RSVP ${s};
d.getElementById("points1").innerHTML =
Friends invited → ${pointStart} / ⇒ ${pointEnd} RSVP ♣;
d.getElementById("points2").textContent =
'Friends invited u2192 '+pointStart+' / u21d2 '+pointEnd+' RSVP '+h;
d.getElementById("points3").textContent =
Friends invited u2192 ${pointStart} / u21d2 ${pointEnd} RSVP ${e};
d.getElementById('foo').appendChild(
d.createTextNode(
'Friends invited u2192 ' + pointStart + ' / u21d2 ' + pointEnd + ' RSVP ' + e ));
} ( document ) );
</script>
</body>
</html><i>
</i>
`</CODE>
Personally, I prefer to use the<C>
`create.TextNode()
``0.1.9 — BETA 4.24