#Getting innerText from div

15 messages · Page 1 of 1 (latest)

plush mason
#

anyone know how to display innertext of div, i tried but it just returns an empty string

window.onload = (event) =>{
      var usernames = "willy wonka"
      var privatechannel = document.createElement("div")
      privatechannel.setAttribute("onclick", "refreshdiv()")
      privatechannel.classList.add("friendurl")
      privatechannel.setAttribute("id", "friendurl");
      privatechannel.setAttribute("value", usernames);
      privatechannel.appendChild(document.createTextNode(usernames))

};

function refreshdiv() {
     const usernamed = document.getElementById("friendurl").innerText
     console.log(usernamed)

}
vapid idol
#

Have you tried using textContent instead of innerText?

plush mason
#

yea

#

i also tried innerhtml

vapid idol
#

Is there actually text in the div???

plush mason
#

yep

vapid idol
#

Have you inserted the div into the HTML?

plush mason
#

yea

vapid idol
#

I ask because in the code you shared you're not

plush mason
#

yeah i removed some code since it wasnt really the issue

#

just returns this

#

just an empty string

vapid idol
#
<script>
window.onload = (event) =>{
      var usernames = "willy wonka"
      var privatechannel = document.createElement("div")
      privatechannel.setAttribute("onclick", "refreshdiv()")
      privatechannel.classList.add("friendurl")
      privatechannel.setAttribute("id", "friendurl");
      privatechannel.setAttribute("value", usernames);
      privatechannel.appendChild(document.createTextNode(usernames))
      document.body.appendChild(privatechannel);

      refreshdiv();
};

function refreshdiv() {
     const usernamed = document.getElementById("friendurl").innerText
     console.log(usernamed)

}
</script>

That code works. So assuming you're apppending the element to the HTML it should be working.

plush mason
#

well im appending it to another div then to a li which then goes into a ul

plush mason
#

its all good i sorted it out