1

I'm trying to check first is mystring contains any html tag. If it contains Html tag then I want to remove those tag.

var mystring = "<div>Lorem ipsum dolor sit<p>&nbsp;amet, consectetur &nbsp;&nbsp;</p><p>adipiscing elit. Integer ut euismod magna, nec ullamcorper erat.</p></div>"

from above string I need to remove all html tag and   also . I'm new for these so. Please suggest something

3 Answers3

2
var my_string = '<div class="ExpandedProfilePane-module_textField_delve fabric-module_ms-TextField_delve" contenteditable="true">Lorem ipsum dolor sit<p>&nbsp;amet, consectetur &nbsp;&nbsp;</p><p>adipiscing elit. Integer ut euismod magna, nec ullamcorper erat.</p></div>'

$('#tst').html($(my_string).text());

Check Demo

Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
2

You can use .text() of jQuery like this:

var myString = '<div class="ExpandedProfilePane-module_textField_delve fabric-module_ms-TextField_delve" contenteditable="true">Lorem ipsum dolor sit<p>&nbsp;amet, consectetur &nbsp;&nbsp;</p><p>adipiscing elit. Integer ut euismod magna, nec ullamcorper erat.</p></div>'

alert($(myString).text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Also, using a variable name like var my-string is invalid so you also need to change that one

prtdomingo
  • 971
  • 4
  • 14
  • how can I caheck if string contains HTML – Akshay Dattatray Nangare Feb 07 '17 at 05:05
  • Check the approved answer in this another thread from StackOverflow http://stackoverflow.com/questions/15458876/check-if-a-string-is-html-or-not, it shows an example on how to use Regex to check if your string contains and HTML tag – prtdomingo Feb 07 '17 at 05:15
0
 <!DOCTYPE html>
 <html>
 <head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
 $("button").click(function(){
    $("p").remove(".test");
});
});
</script>

 </head>
  <body>


 <p class="test">This is another paragraph.</p>

 <button>Remove</button>

 </body>
 </html>