0

Stripping off HTML tags from string, having colon causes jQuery to throw error.

var str = 'Sample: <div>HTML <b>Text</b> with <i>tags</i></div>';
$(str).text();

Colon can not be modified with adding slash(es) to value. how can this be avoided. to remove all tags and get plain text including the colon.

Shiladitya
  • 11,210
  • 15
  • 22
  • 33
TinyToon
  • 1
  • 1
  • 1
    You have no opening `
    ` tag - could that be causing the error? Or is this different from the actual string you are using?
    – AJ Richardson Dec 25 '14 at 05:12
  • Trying this in the console, I don't get any errors, but the output does not appear correct: `"Text with tags"` – ssube Dec 25 '14 at 05:16

2 Answers2

2

You can do it via javascript only:-

var str = 'Sample: <div>HTML <b>Text</b> with <i>tags</i></div>';

function stripAllHtmlTag(str){
        var tmp = document.createElement("DIV");
        tmp.innerHTML = str;
        str = tmp.textContent || tmp.innerText || "";
        console.log(str);
    
}
stripAllHtmlTag(str)

Orginal Answer

If you want to do it via jQuery:

var str = 'Sample: <div>HTML <b>Text</b> with <i>tags</i></div>';
console.log($("<div>").append(str).text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Community
  • 1
  • 1
Gaurav Kalyan
  • 1,857
  • 2
  • 12
  • 20
0

Put opening div and check it work fine

var str = '<div>HTML <b>Text</b> with <i>tags</i></div>';
alert("Sample : "+$(str).text());

JS Fiddle

Sadikhasan
  • 17,212
  • 19
  • 72
  • 111
  • The text i am getting is dynamic so Sample can not be separated. Colon position is also variable but currently the HTML after colon is causing issues. can you try on the sample in question without dividing it. – TinyToon Dec 25 '14 at 06:14