Reputation: 742
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 <b />
So I need to change it back to the HTML characters using only jQuery:
<
to <
>
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"><strong>Valuación</strong> de InfoAuto: 35.500,00<br />
Cotización Seleccionada: Ninguna<br />
Allianz, Responsabilidad Civil: $205,25<br />
Allianz, Terceros Completos: $278,85 </div>
Upvotes: 22
Views: 116692
Reputation: 1
I made a small tool to covert HTML text containing < and > to <
and >
So you can use this tool to convert your code.
I made it using simple replaceAll()
method.
elem.replaceAll('&', '&').replaceAll('<','<').replaceAll('>', '>')
Upvotes: -1
Reputation: 3501
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('<','<').replace('>', '>'));
});
Upvotes: 0
Reputation: 950
All of the above didn't really work for me because what I needed was something to replace all <
to < and >
to > , not only the first one.
What I did was:
.split('<').join('<').split('>').join('>');
Just thinking out of the box here. It worked for me, I hope it does for you too.
Upvotes: 2
Reputation: 2834
Try This:-
var wrapper=$(".contentwrap").html();
wrapper=wrapper.replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');
$(".contentwrap").html(wrapper);
Upvotes: 3
Reputation: 637
<?php
$a =
'<div style="font-size: 11px; width: 90%; font-family: Tahoma;" id="cotiz"><strong>Valuación</strong> de InfoAuto: 35.500,00<br />
Cotización Seleccionada: Ninguna<br />
Allianz, Responsabilidad Civil: $205,25<br />
Allianz, Terceros Completos: $278,85 </div>';
$b = html_entity_decode($a);
echo $b;
?>
Upvotes: -1
Reputation: 241
Please try this
.replace(/</g, '<').replace(/>/g, '>')
to replace these characters globally. I tried this and works like a charm :)
Upvotes: 24
Reputation: 466
I have different solution then the conventional, and it will be applied to decode/encode html
Decode
var encodedString = "<Hello>";
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 "<Hello>" this string
Upvotes: 16
Reputation: 50976
The simplest thing to do would be
$('#test').each(function(){
var $this = $(this);
var t = $this.text();
$this.html(t.replace('<','<').replace('>', '>'));
});
working edit/jsfiddle by Jared Farrish
Upvotes: 13
Reputation: 359816
$('#myDivId').text(function (i, text)
{
return text.replace('<', '<').replace('>', '>');
});
Upvotes: 5