TinyToon
TinyToon

Reputation: 1

jQuery strip html tags colon issue

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.

Upvotes: 0

Views: 233

Answers (2)

Gaurav Kalyan
Gaurav Kalyan

Reputation: 1897

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>

Upvotes: 1

Sadikhasan
Sadikhasan

Reputation: 18600

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

Upvotes: 0

Related Questions