There is a function that stupidly takes the whole page as text:

function handleServerResponse() { var response = xmlHttp.responseText; myDiv = document.getElementById('myDivElement'); var VRegExp = new RegExp(/<pre class="data">.*<\/pre>/); var VResult = response.match(VRegExp); myDiv.innerHTML = VResult; } 

How to make so that from response then get the value of a specific div, which belongs to a particular class, and display on the page?

  • If you are given an exhaustive answer, mark it as correct (tick the selected answer). - Mihanik71 6:22 pm

1 answer 1

There are 2 options.

The first is with a regular expression. This option will not work if in a block (for example a div) there will be nested blocks with the same tag (div).

 function handleServerResponse() { var response = xmlHttp.responseText; myDiv = document.getElementById('myDivElement'); var VRegExp = new RegExp(/<div.* class="test".*>.*<\/div>/);//регулярное выражение var VResult = response.match(VRegExp);//поиск строки myDiv.innerHTML = VResult; } 

The second is to simply insert everything into an invisible block (display: none), then find the necessary block through the DOM and insert the values ​​where necessary.

The second option:

 function handleServerResponse() { var response = xmlHttp.responseText; myDiv = document.getElementById('myDivElement'); myDiv.innerHTML = "<div style='display:none'>"+response+"</div>"; myDiv.innerHTML = myDiv.querySelector("pre.data").innerHTML; } 
  • that is, from the container with the pre class, I can extract it this way: var VRegExp = new RegExp (/ <div. * class = "test". *>. * <\ / div> /) ;? - guitarhero
  • In this case, I wrote a regular page to select a tag (open and close) and the contents of a diva with the class test - Mihanik71
  • @guitarhero apologize. Mistake was. Corrected and wrote near the added lines what they do - Mihanik71
  • Yes, I didn’t send it there either) unfortunately it doesn’t help :( what does “. *” mean in the expression? - guitarhero
  • to make it clearer: I need to take a number from these tags and insert it into my container - <pre class = "data"> 82 </ pre> - guitarhero