All TalkersCode Topics

Follow TalkersCode On Social Media

Remove HTML Tags From String JavaScript

Last Updated : Jan 1, 2023

Remove HTML Tags From String JavaScript

In this tutorial we will show you the solution of remove HTML tags from string JavaScript, mainly there are many methods with help of which we are able to remove tags. Let us understand them one by one.

As, there are methods like replace(), textcontent() and innerText(). These all are inbuilt or predefined function with which we can remove tags.

Step By Step Guide On Remove HTML Tags From String JavaScript :-

In this article we are going to use replace() to remove tags from string. In next sessions we will understand other methods and understand how they work. Let us see the codes below to understand replace().

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>convert object to array using JavaScript</title>
function removeHtml(string) {
 if ((string===null) || (string===''))
  return false;
  string = string.toString();
 return string.replace( /(<([^>]+)>)/ig, '');
 '<html> Hello World! </html>'));
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, as we can see that we directly use script tag to write our JavaScript code there and as we know that JavaScript is written under script tag, script tag is a paired tag and must have a closing tag.
  6. Here, we create a function with name removeHtml and pass an argument string there. If our string is null or empty it return false else the function runs.
  7. This all we done with help of if condition. In else our string is converted to string format using toString() and then we use striing.replace() to remove the html tags under JavaScript.
  8. After that we use our function removeHtml that we created above and pass a string inside this.
  9. We you see the output you see only Hello World! You see that the html tags that we created there gets removed. We hope that you understand the codes properly with help of these steps.
  10. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to remove tags from string JavaScript.

I hope this tutorial on remove HTML tags from string JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials