6

How do I get a value from a 'pre' tag?

<pre>43453453</pre>

For example, I know how to get the tag, but how do I get the content inside it?

var x = documentGetElementsByTagName("pre");

I tried using innerHTML, and textContent, but those didn't work for me.

userden
  • 1,615
  • 6
  • 26
  • 50

1 Answers1

11

document.getElementsByTagName will return a NodeList and not first element. So you will have to loop over list and fetch manually.

If its just one element, you can even look into document.querySelector. This will give you first element of given selector

var _html = document.getElementsByTagName('pre')[0].innerHTML;
console.log(_html)

var _query = document.querySelector('pre').innerHTML
console.log(_query)
<pre>43453453</pre>
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • Thank you! Out of interest, why do you use the underscore in front of the variable names? – userden Oct 30 '16 at 07:49
  • 1
    Just a convention. `_` before variable name can be used to depict `private variable`. Similarly, all caps mean `constant/global variable` – Rajesh Oct 30 '16 at 07:51