If you’re running in a browser, then the easiest way is just to let the browser do it for you…
function stripHtml(html) { let tmp = document.createElement("DIV"); tmp.innerHTML = html; return tmp.textContent || tmp.innerText || ""; }
Note: as folks have noted in the comments, this is best avoided if you don’t control the source of the HTML (for example, don’t run this on anything that could’ve come from user input). For those scenarios, you can still let the browser do the work for you – see Saba’s answer on using the now widely-available DOMParser.
Related Posts:
- Failed to load resource: the server responded with a status of 404 (Not Found)
- How to check whether a string contains a substring in JavaScript?
- Maximum call stack size exceeded error
- Google Chrome Uncaught (in promise) DOMException while playing AUDIO
- How to make a word underline in Markdown
- Creating multiline strings in JavaScript
- ChartJS beginAtZero, min, max doesn’t work
- how to fix : ” TypeError: Cannot read property ‘addEventListener’ of null”…?//
- Cannot set property InnerHTML of null [duplicate]
- ReferenceError: $ is not defined
- React.js: Set innerHTML vs dangerouslySetInnerHTML
- How do you reverse a string in-place in JavaScript?
- jQuery.click() vs onClick
- Add swipe left/right to web page, but use default swipe up/down
- Check if checkbox is checked with jQuery
- What is the correct way to check for string equality in JavaScript?
- What is the meaning of == $0 that is shown in inspect element of google chrome for the selected element [duplicate]
- Angular: mat-form-field must contain a MatFormFieldControl
- Check if a variable is a string in JavaScript
- Change Background color (css property) using Jquery
- Failed to load resource: net::ERR_FILE_NOT_FOUND loading json.js
- Get class name using jQuery
- How to clear the canvas for redrawing
- JavaScript get element by name
- Use images like checkboxes
- Completely disable scrolling of webpage
- TypeError: Cannot Set property ‘onclick’ of null
- How to print elements from array with javascript
- How do I scroll to an element using JavaScript?
- display variable image using cookies
- character counter – backspace doesn’t reflect on characters remaining
- Open URL in same window and in same tab
- Create table with jQuery – append
- How to convert selected HTML to Json?
- How would I call a javascript function in html?
- How to Open New window on every Click
- “.addEventListener is not a function” why does this error occur?
- Cannot read property ‘getContext’ of null, using canvas
- How to move an element after another element using JS or jquery?
- How to use Javascript to read local text file and read line by line?
- Why is the jquery script not working?
- Count the number of occurrences of a character in a string in Javascript
- DOMException: Failed to load because no supported source was found
- Anagrams finder in javascript
- How to interpolate variables in strings in JavaScript, without concatenation?
- Textarea Auto height
- Partial render in HTML/JavaScript
- How to add onload event to a div element
- String.Format not work in TypeScript
- How to do case insensitive string comparison?
- Print the contents of a DIV
- How to create a HTML Cancel button that redirects to a URL
- Showing an image from an array of images – Javascript
- Embedding instagram webpage inside an iframe
- Set value to currency in
- HTML Entity Decode
- why angularjs ng-repeat not working
- Scroll to bottom of div?
- Clearing my form inputs after submission
- HTML5 rich-text inside textarea
- Escaping HTML strings with jQuery
- How do I check if a cookie exists?
- Highlight a word with jQuery
- How to change the buttons text using javascript
- How do I refresh a DIV content?
- how to display a div triggered by onclick event
- jQuery sort elements using data id
- getContext is not a function
- JavaScript code to stop form submission
- How to change the text of a label?
- Change an image with onclick()
- JavaScript – Count Number of Visitor for Website
- Using “×” word in html changes to ×
- How do I store an array in localStorage?
- Fire oninput event with jQuery
- Changing the selected option of an HTML Select element
- img onclick call to JavaScript function
- How to scroll to top of page with JavaScript/jQuery?
- Check if a JavaScript string is a URL
- Calling a JavaScript function in another js file
- How to embed an autoplaying YouTube video in an iframe?
- JavaScript code not running in HTML5 document
- Cannot read property ‘style’ of undefined — Uncaught Type Error
- Unexpected token < in first line of HTML
- How to make HTML element resizable using pure Javascript?
- How to set “style=display:none;” using jQuery’s attr method?
- WordPress Bootstrap Handburger Menu Wont Open
- The value of attribute [ data-type ] must be in double quotes – custom html widget error
- Adding JavaScript to a WordPress website
- Insert “javascript:void(0);” into URL
- How to display post content in the block editor
- My Button Default Click Is Not Working In WordPress Using Javascript Code
- Make a script work within a page
- how to let users upload their custom cover image
- Putting custom html/js page into Elementor as it’s own block
- Add Paypal button redirect to a specific page after completing
- Can’t Listen to KeyDown in TInyMCE Iframe (jQuery) and Pass it to Parent HTML FORM
- Making the HTML list to a checkbox tree with the plugin jstree [closed]
- dropdown does not work [closed]
- How do I cycle a JS function in WordPress? [closed]