question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Template literals

See original GitHub issue

I am trying to render a form-like template as shown below. image

This method below works fine, however it is very manual which is not ideal for the form that I will be creating which will have many more text inputs.

document.getElementById("name").value = "<%= name; %>";
document.getElementById("margin").value = "<%= margin; %>";
document.getElementById("mileage").value = "<%= mileage; %>";

I have tried 2 methods to automate my problem:

<% for (var i = 1; i < Object.keys(info).length; i++) { %>
            <% let x = Object.keys(info)[i]; %>
            <label for=<% x %>><%= x %></label>
            <input type="text" id="<= x %>" value="<% info[x] %>" ><br>
        <% } %>

This works except for the fact that when I reload the page the local state is preserved and the value of the text input does not return back to info[x].

My other method was to loop through through each text input and apply the value as such;

const elementsList = document.querySelectorAll("#id, #name, #margin, #mileage, #max_price, #min_price");
        const elementsArray = [...elementsList];

        elementsArray.forEach((i) => {
            document.getElementById(i).value = `<% info[${i}] %>`
        });

However the problem with this is that <% info[${i}] %> does not format the i into the template literal, but instead raises SyntaxError: Unexpected token '{' .

I have read multiple issues similiar to this however none of them seemed to help. Does anyone have any ideas how I might be able to achieve an automated text input with value of the ejs variables without manually writing out each value?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mdecommented, Jun 17, 2022

Given that the persistence of client-side state is a browser issue, and not an EJS issue, it seems like your solution is actually the optimal one.

0reactions
CaedenPHcommented, Jun 17, 2022

I have found a solution which is probably not the most ideal but it works.

<% for (var i = 1; i < Object.keys(info).length; i++) { %>
            <% let x = Object.keys(info)[i]; %>
            <label for=<% x %>><%= x %></label>
            <input type="text" id="<%= x %>" value="<%= info[x] %>"><br>
        <% } %>

This renders the inputs and the values on the input, and in the javascript of that page I added

document.getElementById("form").reset();

This means that the form is reset to the values passed into the html.

Thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

Template literals (Template strings) - JavaScript | MDN
Template literals are enclosed by backtick ( ` ) characters instead of double or single quotes. Along with having normal strings, template ...
Read more >
JavaScript Template Literals - W3Schools
Template literals provide an easy way to interpolate variables and expressions into strings. The method is called string interpolation. The syntax is:.
Read more >
ES6 Template Literals (Template Strings) - CanIUse
Template literals are string literals allowing embedded expressions using backtick characters (`). You can use multi-line strings and string interpolation ...
Read more >
8. Template literals - Exploring JS
A template literal is a new kind of string literal that can span multiple lines and interpolate expressions (include their results). For example:...
Read more >
JavaScript Template Literals
This tutorial shows you how to use JavaScript template literals to manage literal templates in a cleaner and more effective way.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found