Answers for "javascript covert html characters to text"

1

js Convert the characters to the html

//To do this simply create a element in the DOM tree and 
//set the innerText of the element to your string. 
//Then retrieve the innerHTML of the element. 
//The browser will return an HTML encoded string.

function HtmlEncode(s)
{
  var el = document.createElement("div");
  el.innerText = el.textContent = s;
  s = el.innerHTML;
  return s;
}

console.log(HtmlEncode('&;\'><"'));

//expected output: &amp;;'&gt;&lt;"
Posted by: Guest on August-21-2021
0

javascript covert html characters to text

const decodeHTML = s => {
    var str, temp= document.createElement('p');
    temp.innerHTML= s;
    str= temp.textContent || temp.innerText;
    temp=null;
    return str;
}

console.log(decodeHTML('&lt;'));
Posted by: Guest on September-21-2021

Code answers related to "javascript covert html characters to text"

Code answers related to "Javascript"

Browse Popular Code Answers by Language