0

This is the jQuery code

success: function (html) {
         var text = $(html).text();
         if (text == '') {
            location.reload();
         } else {
            $("#warning2").html(html);

         }
 }

What I want is to convert $(html).text(); to plain JavaScript

Here is my plain JavaScript code but the page will not reload unlike jQuery code.

xhttp.onreadystatechange = function() 
{
    if (xhttp.readyState == 4 && xhttp.status == 200)
       {
       if (xhttp.responseText == "" || xhttp.responseText.textContent == "")
         location.reload();
       else
         document.getElementById("warning2").innerHTML = xhttp.responseText;
      }
};
2

1 Answer 1

0

Based on your description:

"What I want is to convert $(html).text(); to plain JavaScript"

So, I suppose you have a correct ajax function and your question is about converting that part of your code to pure js. (If you haven't it, you can check stackoverflow for links like this ).

Your code in jquery:

var text = $(html).text();

Same code in js:

var uDiv=document.createElement("div");
uDiv.innerHTML= xhttp.responseText;
var text=uDiv.children[0].innerText; //or .innerHTML

tip: in your last line (.innerHTML=...), if the response has script tags, you must separate them and create that script tags via code (similar my above code).

Sign up to request clarification or add additional context in comments.

2 Comments

i change some of your codes to make it work. my failure was i did not build an element.
This was a trick. We don't append element to document never. I'm glad the problem solved.

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.