Suomi Suomi - 1 month ago 4
Javascript Question

How to get the contents of a pre tag with JavaScript?

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


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.


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;

var _query = document.querySelector('pre').innerHTML