Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Replace &lt; with < and &gt; with > using jquery

Tags:

I have a page that is part of a backend CRM admin panel. On that page the HTML output comes from some PHP functions that I can't access. And that HTML automatically changes < and > into HTML encoded characters.

So there is a div that contains html tags like <br /> that is converted into &lt;b /&gt;

So I need to change it back to the HTML characters using only jQuery:

&lt; to <
&gt; to >

Is there a jQuery script I can use to replace those special characters with the corresponding symbols? This will mean my HTML tags will actually work and the HTML will being displayed properly on the screen?

I've tried removewith() but i can't make it work.

ADDED: The div that im trying to modify is this

<div style="font-size: 11px; width: 90%; font-family: Tahoma;" id="cotiz">&lt;strong&gt;Valuación&lt;/strong&gt; de InfoAuto: 35.500,00&lt;br /&gt;  Cotización Seleccionada: Ninguna&lt;br /&gt;  Allianz, Responsabilidad Civil: $205,25&lt;br /&gt;  Allianz, Terceros Completos: $278,85 </div> 
like image 871
sebas Avatar asked Jul 22 '11 00:07

sebas


2 Answers

Please try this

.replace(/&lt;/g, '<').replace(/&gt;/g, '>')  

to replace these characters globally. I tried this and works like a charm :)

like image 51
Kiran Banda Avatar answered Sep 17 '22 20:09

Kiran Banda


I have different solution then the conventional, and it will be applied to decode/encode html

Decode

var encodedString = "&lt;Hello&gt;"; var decodedText = $("<p/>").html(encodedString).text();  /* this decodedText will give you "<hello>" this string */ 

Encode

var normalString = "<Hello>"; var enocodedText = $("<p/>").text(normalString).html(); /* this encodedText will give you "&lt;Hello&gt;" this string 
like image 30
AdiechaHK Avatar answered Sep 21 '22 20:09

AdiechaHK