How To Replace < with < and > with > using jquery
Asked Answered
S

11

22

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>
Sufflate answered 22/7, 2011 at 0:50 Comment(4)
Just on the off chance, are you using .text() to insert the data instead of .html()?Janettajanette
possible duplicate of How do I find and replace HTML entities with jQuery?Shavon
@Shavon - Please note the capitalization in the above comment. ;)Burghley
This is some nefarious stuffEquitant
S
12

The simplest thing to do would be

$('#test').each(function(){
    var $this = $(this);
    var t = $this.text();
    $this.html(t.replace('&lt','<').replace('&gt', '>'));
});

working edit/jsfiddle by Jared Farrish

Shavon answered 22/7, 2011 at 0:56 Comment(6)
Not quite. jsfiddle.net/XS5kY Note, I have fixed a couple of issues with the above for demo purposes.Burghley
I see that its working on that jsfiddle demo but its not working on my setup...the div reference is correct btw. What could be the problem?Sufflate
@sebas: I have no idea. show us your whole #test element in your questionShavon
just posted the div that your jquery cant modify in my setup for some reason.Sufflate
SORRY edited again the question pleae check the div now as it is exactly as i can see it in the html code with the special characters that i need to convert into < and >Sufflate
i wasnt using document.ready and an evil php function kept changing the characters now it works. you da man!Sufflate
F
24

Please try this

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

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

Fogg answered 24/1, 2012 at 8:19 Comment(0)
S
16

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
Secrecy answered 15/10, 2013 at 12:57 Comment(1)
Do you have a plain javascript alternative?Oscar
S
12

The simplest thing to do would be

$('#test').each(function(){
    var $this = $(this);
    var t = $this.text();
    $this.html(t.replace('&lt','<').replace('&gt', '>'));
});

working edit/jsfiddle by Jared Farrish

Shavon answered 22/7, 2011 at 0:56 Comment(6)
Not quite. jsfiddle.net/XS5kY Note, I have fixed a couple of issues with the above for demo purposes.Burghley
I see that its working on that jsfiddle demo but its not working on my setup...the div reference is correct btw. What could be the problem?Sufflate
@sebas: I have no idea. show us your whole #test element in your questionShavon
just posted the div that your jquery cant modify in my setup for some reason.Sufflate
SORRY edited again the question pleae check the div now as it is exactly as i can see it in the html code with the special characters that i need to convert into < and >Sufflate
i wasnt using document.ready and an evil php function kept changing the characters now it works. you da man!Sufflate
D
5
$('#myDivId').text(function (i, text)
{
    return text.replace('&lt;', '<').replace('&gt;', '>');
});
Dehorn answered 22/7, 2011 at 0:55 Comment(0)
A
4

if use underscore.js exist _.unescape(string)

Aphoristic answered 24/4, 2014 at 10:42 Comment(0)
D
4

Use $this.html('...'); instead $this.text('...');

Dismount answered 6/2, 2017 at 8:20 Comment(0)
N
3

Try This:-

var wrapper=$(".contentwrap").html();
  wrapper=wrapper.replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&');
 $(".contentwrap").html(wrapper);
Nelidanelie answered 22/6, 2017 at 9:21 Comment(0)
T
2

All of the above didn't really work for me because what I needed was something to replace all &lt; to < and &gt; to > , not only the first one. What I did was:

.split('&lt;').join('<').split('&gt;').join('>');

Just thinking out of the box here. It worked for me, I hope it does for you too.

Townsman answered 1/3, 2018 at 14:36 Comment(0)
K
0

I needed to step to find an H1 then parse the next element, because I wasn't able to use a specific ID like you did. Good tip!

$('#sscContent').find("h1").next().each(function(){
    var $this = $(this);
    var t = $this.text();
    $this.html(t.replace('&lt;','<').replace('&gt;', '>'));
});
Kenney answered 29/5, 2019 at 17:9 Comment(0)
M
-1

You can do it simply with php

<?php
$a =
 '<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>';

$b = html_entity_decode($a);


echo $b;
?>
Merrie answered 16/11, 2016 at 9:44 Comment(1)
The question specifically says using jquery! Please read the question through first!Renaldorenard
I
-1

I made a small tool to covert HTML text containing < and > to &lt; and &gt;

So you can use this tool to convert your code.

I made it using simple replaceAll() method.

elem.replaceAll('&', '&amp;').replaceAll('<','&lt;').replaceAll('>', '&gt;')
Insertion answered 8/3, 2022 at 16:9 Comment(1)
OP wants to replace the other way aroundCosmic

© 2022 - 2024 — McMap. All rights reserved.