Is it possible to append a template string to a div? I'm trying to optimise my code and currently my code is having to create element, add classes, add a text node and then append all together. I was wondering if its possible to just create a template string and then append that, to be more efficient? I don't want to wipe out the current div's html with innerHTML.
socket.on('coinFlip', (result) => {
const messages = document.querySelector('#messages');
const output = `<li class="message broadcast">${result.result}</li>`;
messages.appendChild(output);
});
Current code that works is:
socket.on('coinFlip', (result) => {
// Grab dialog box
const messages = document.querySelector('#messages');
// Render messages
const li = document.createElement('li');
li.className = "message broadcast";
const text = document.createTextNode(`${result.result}`);
li.appendChild(text);
messages.appendChild(li);
});
When you use regular template literals, your input is passed to a default function that concatenates it into a single string. An interesting thing is that you can change it by preceding the template literal with your function name that acts as a tag.
Template literals are literals delimited with backtick ( ` ) characters, allowing for multi-line strings, string interpolation with embedded expressions, and special constructs called tagged templates.
Although single quotes and double quotes are the most popular, we have a 3rd option called Backticks ( `` ). Backticks are an ES6 feature that allows you to create strings in JavaScript. Although backticks are mostly used for HTML or code embedding purposes, they also act similar to single and double quotes.
In ES6, we can now use template strings. I like to use these multiline strings when I'm creating HTML markup as a string. You can use backticks here, and use the object properties as variables. These look familiar because they're in the same format as I've been using in some of the previous posts.
Use Element.insertAdjacentHTML(position, text)
The
insertAdjacentHTML()
method of the Element interface parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position. It does not reparse the element it is being used on, and thus it does not corrupt the existing elements inside that element. This avoids the extra step of serialization, making it much faster than direct innerHTML manipulation.Parameters
position
ADOMString
representing the position relative to theelement
;
must be one of the following strings:
-'beforebegin'
: Before theelement
itself.
-'afterbegin'
: Just inside theelement
, before its first child.
-'beforeend'
: Just inside theelement
, after its last child.
-'afterend'
: After theelement
itself.
text
The string to be parsed as HTML or XML and inserted into the tree.
socket.on('coinFlip', (result) => {
const messages = document.querySelector('#messages');
const output = `<li class="message broadcast">${result.result}</li>`;
messages.insertAdjacentHTML("beforeend", output);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With