javascript document.getElementById not working from db

Viewed 121

I'm sure I'm missing something silly here, but I cannot seem to figure it out.

I am basically trying to get the names from my database, which is working fine (I can show them on screen), but when I try to get them and store them in a variable, it doesn't work.

here's the code:

<% if(data.length){ 
for(var i = 0;i < data.length;i++) { %>
    <p id="names"><%= data[i].FULLNAME %></p>
    <% }
           }else{ %>
        <p>nothing</p>
    <% } %>
  <script>
      var listOfNames = document.getElementById("names").value
      console.log("list of names" + listOfNames)
      

I now need a new line after every name, instead of beside each other one by one, like so: enter image description here

thanks for the help in advanced.

1 Answers

You need to use innerText instead of value

const listOfNames = document.getElementById("names").value
console.log("list of names: " + listOfNames)

const correctText = document.getElementById("names").innerText
console.log("list of names: " + correctText)
<p id="names">whatever</p>

Related