Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending a template string

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);

});
like image 915
Stephanie Parker Avatar asked Feb 10 '19 16:02

Stephanie Parker


People also ask

Can you concatenate template literals?

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.

What is a template string?

Template literals are literals delimited with backtick ( ` ) characters, allowing for multi-line strings, string interpolation with embedded expressions, and special constructs called tagged templates.

What is `` in JS?

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.

Can we use template string in HTML?

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.


1 Answers

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
    A DOMString representing the position relative to the element;
    must be one of the following strings:
        - 'beforebegin': Before the element itself.
        - 'afterbegin': Just inside the element, before its first child.
        - 'beforeend': Just inside the element, after its last child.
        - 'afterend': After the element 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);

});
like image 117
Andreas Avatar answered Oct 21 '22 21:10

Andreas