Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace &amp to & , <lt to < and >gt to gt in javascript

Tags:

javascript

I want to replace &amp to & using javascript. Here is the sample code of mine.EmployeeCode could contain &. The EmployeeCode is selected from Datagrid and its showed in "txtEmployeeCode" textbox. But if the EmployeeCode contains any & then it shows &amp into the textbox. How could &amp be removed from EmployeeCode? can anyone help...

function closewin(EmployeeCode) {
     opener.document.Form1.txtEmployeeCode.value = EmployeeCode;
     this.close();
}
like image 424
rafat Avatar asked Jan 07 '14 05:01

rafat


1 Answers

With this:

function unEntity(str){
   return str.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">");
}

function closewin(EmployeeCode) {
     opener.document.Form1.txtEmployeeCode.value = unEntity(EmployeeCode);
     this.close();
}

OPTIONAL If you are using jQuery, this will decode any html entity (not only &amp; &lt; and &gt;):

function unEntity(str){
   return $("<textarea></textarea>").html(str).text();
}

Cheers

like image 110
Edgar Villegas Alvarado Avatar answered Sep 28 '22 07:09

Edgar Villegas Alvarado