Use javascript inside an EJS file to display array information

Asked

Viewed 576 times

1

I’m trying to show inside an EJS/html file the values of an array that are sent from the server, but I’m not getting it. I know that to show the value of a variable inside an EJS file all I need to do is put the variable name inside the tags <=% %>, as an example: <%= helloWord %>, but now I’m trying to run a loop for inside this tag to show the data of an array, but it’s not working. Does anyone know how to make it work? Follow below page code:

<!DOCTYPE html>
<html>
  <head>
    <title>Página Inicial</title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h3><%= artwork %></h3>
    <center>
      <img src="images/IMG01.jpg" alt="Imagem da Obra de Arte 1" title="Obra de Arte 1"></br>
    </center>

    <div class="container">
      <p id="message">
        <%=  
          for(let i = 0; i < 10; i++){
            log[i];
          }

        %>
      </p>


      <form method="POST">
        <input type="text" name="inputQuestion" placeholder="Pergunte-me" required />
        <button>Enviar</button>
      </form>

    </div>

  </body>
</html>

1 answer

0


For processing you use <% %>, and for printing you use <%= %>.

Then it’ll stay that way:

<% for(let i = 0; i < 10; i++) { %>
  <%= log[i] %>
<% } %>
  • I get it. Just one more thing, if I wanted the value of i within the for is up to the size of the array, as I should then put?

  • would be like in javascript itself, "for(Let i = 0; i < log.length; i++)"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.