1

here is the my code code :

const http = new XMLHttpRequest();
const url  = 'http://page/';

http.open('get', url, false);
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

http.onreadystatechange = function ()
{
  if(http.readyState === 4)
  {
    if(http.status === 200 || http.status == 0)
    {
        let str = (http.responseText);
        results = document.getElementById('test').value;
        alert(results)
    }
  }
}

http.send();

how i can sort the str into html so i can get the value using document.getElementById ?

i have tried

var doc = document.implementation.createHTMLDocument("example");

doc.documentElement.innerHTML = http2.responseText; 

let test1 = doc.body.querySelector("#test").value;

it worked but it give me an empty value and when i looked at the response preview i found the value bar empty and the options are like in the factory default status . and when i looked in the html code of the page found the value in this line

<script language=javascript>Transfer_meaning('test','\12345678');</script>
12
  • If test is in the the response you would need to add the html to the document before calling getElementById Commented Jun 20, 2019 at 23:20
  • What kind of HTML element carries the id test? If it is eg. an input element, setting the innerHTML does not magically generate a valueattribute ! Also check out what the response contains and check out the mime type that is delivered ( console.log('type: ' + responseType); in JS ). Commented Jun 20, 2019 at 23:39
  • so, you want to get this value \12345678 ? Commented Jun 20, 2019 at 23:53
  • @MisterJojo yes but without that \ Commented Jun 20, 2019 at 23:59
  • @collapsar yes it's an input element Commented Jun 21, 2019 at 0:00

2 Answers 2

0

If responseText is a plain text HTML, you could do this

document.getElementById('test').innerHTML = str;
Sign up to request clarification or add additional context in comments.

17 Comments

i have tried it but didn't work let str2 = (http2.responseText); document.querySelector("#test").innerHTML = str2; got Cannot set property 'innerHTML' of null . while this worked ` var doc = document.implementation.createHTMLDocument("example"); doc.documentElement.innerHTML = http2.responseText; let test1 = doc.body.querySelector("#test").value;` but it give me an empty value ! and when i looked at the response preview i found the value bar empty and the options are like in the factory default status . and when i looked in the
We can't help you if you don't show us the code. What is inside http.responseText?
html code of the page found the value in this line <script language=javascript>Transfer_meaning('test','\12345678');</script>
@MisterJojo what now ?
It's incomprehensible.
|
0

If you were using jquery:

Provided the response was something like:

let str = "<body><p>Paragraph Here</p><input id='test' value='foo'></body>";

Replace this line:

results = document.getElementById('test').value;

with this line:

let results = $('input#test', "<div>"+str+"</div>").val(); 

From this answer

2 Comments

unfortunately i am not using jquery
@minanageh Consult this SO answer on how to load jquery ad-hoc without editing the page html directly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.