var xhr = new XMLHttpRequest(); xhr.open("GET", "https://vk.com/1i_1_i_1", true); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { var data = xhr.responseText; var online = data.getElementById("profile_online_lv"); dannie.innerHTML = online; } } xhr.send(); 

I have this code here. I want to get some text from a tag and paste it into a popup.html extension. The problem is that the getElementById method (and similar ones) does not work, and in the console it produces the error "getElementById is not a function". Although if you just insert the entire page in the popup, then everything works. dannie.innerHTML = data; How to fix? Since yesterday I have been racking my brains, 100 times already rewrote this method)

  • You insert your text in a hidden field, so that it becomes an element of the house, and then ask to find it by id - splash58
  • @ splash58 I didn’t quite understand what kind of text and what hidden field. (Sorry if I'm drowning). - ThreegunD

1 answer 1

Hidden field in html:

 <div style='display: none' id='example'> </div> 

We insert the information that you received:

 if (xhr.readyState == 4) { var data = xhr.responseText; var example = document.getElementById('example'); example.innerHTML = data; var online = document.getElementById("profile_online_lv"); dannie.innerHTML = online; } 

Should work

  • for sure, the "data" itself stores only the link, and not the document itself. Such fundamentals are forgotten: right though, did I understand something? - ThreegunD
  • Yes, and besides, get ... methods apply only to DOM nodes - Alexander Bondarenko
  • @ThreegunD, in this case data is a string, not a link / document - Grundy
  • one
    @Alexander Bondarenko, you can not have divs in the markup, but simply create it when you receive data - Grundy
  • @Alexander Bondarenko, now I’m completely confused. Don't we get a full DOM document by request? - ThreegunD