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?
- Check if a string has a certain piece of text [duplicate]
- Failed to load resource: the server responded with a status of 404 (Not Found)
- How to convert a string to an integer in JavaScript?
- Maximum call stack size exceeded error
- How to properly use jsPDF library
- How can I do string interpolation in JavaScript?
- Can I use complex HTML with Twitter Bootstrap’s Tooltip?
- How do I make the first letter of a string uppercase in JavaScript?
- How do I make the first letter of a string uppercase in JavaScript?
- 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 do I check whether a checkbox is checked in jQuery?
- how to fix : ” TypeError: Cannot read property ‘addEventListener’ of null”…?//
- Cannot set property InnerHTML of null [duplicate]
- ReferenceError: $ is not defined
- How to fix “Cannot read property ‘addEventListener’ of null” error [duplicate]
- React.js: Set innerHTML vs dangerouslySetInnerHTML
- How do I check whether a checkbox is checked in jQuery?
- jQuery document.createElement equivalent?
- (Google Map API) Geocode was not successful for the following reason: REQUEST_DENIED
- Creating multiline strings in JavaScript
- Split string into array
- How to make a word underline in Markdown
- Pure JavaScript equivalent of jQuery’s $.ready() – how to call a function when the page/DOM is ready for it [duplicate]
- How to play audio?
- How to play audio?
- How do you reverse a string in-place in JavaScript?
- How can I scroll to an element using jQuery?
- jQuery.click() vs onClick
- jQuery Get Selected Option From Dropdown
- Include another HTML file in a HTML file
- Check if checkbox is checked with jQuery
- Add swipe left/right to web page, but use default swipe up/down
- How to add jQuery code into HTML Page
- How can I get the data-id attribute?
- Check if checkbox is checked with jQuery
- What is the correct way to check for string equality in JavaScript?
- How to resolve the C:\fakepath?
- What is the meaning of == $0 that is shown in inspect element of google chrome for the selected element [duplicate]
- Check if a variable is a string in JavaScript
- What’s the best way to convert a number to a string in JavaScript?
- Angular: mat-form-field must contain a MatFormFieldControl
- Check if a variable is a string in JavaScript
- (change) vs (ngModelChange) in angular
- Bootstrap Dropdown menu is not working
- jquery SlideToggle effect in upward direction?
- 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
- Adding options to select with javascript
- Export html table data to Excel using JavaScript / JQuery is not working properly in chrome browser
- Set timeout for ajax (jQuery)
- How can I remove a character from a string using JavaScript?
- How does the data-toggle attribute work? (What’s its API?)
- CSS height 100% percent not working
- JavaScript get element by name
- Use images like checkboxes
- How to read xml file contents in jQuery and display in html elements?
- HTML5 Local storage vs. Session storage
- Completely disable scrolling of webpage
- TypeError: Cannot Set property ‘onclick’ of null
- How to print elements from array with javascript
- Express Render not working return error: No default engine was specified and no extension was provided
- How do I scroll to an element using JavaScript?
- Jquery swipe left or right on mobile
- Programmatically change the src of an img tag
- display variable image using cookies
- How to move an element into another element?
- character counter – backspace doesn’t reflect on characters remaining
- How to remove text from a string?
- Open URL in same window and in same tab
- Create table with jQuery – append
- What does ${} (dollar sign and curly braces) mean in a string in Javascript?
- Error: Failed to execute ‘appendChild’ on ‘Node’: parameter 1 is not of type ‘Node’
- Delete first character of a string in Javascript
- How to convert selected HTML to Json?
- ES6: Create Strings using Template Literals – Freecodecamp
- How would I call a javascript function in html?
- How to Open New window on every Click
- Encode HTML entities in JavaScript
- “.addEventListener is not a function” why does this error occur?
- Uncaught TypeError: Cannot read property ‘top’ of undefined
- Can’t Listen to KeyDown in TInyMCE Iframe (jQuery) and Pass it to Parent HTML FORM
- Hide CSS class only in single post pages
- How to delay display of page elements until enqueued script has injected html
- how to do open a link in a open window from menu bar? [closed]
- Making the HTML list to a checkbox tree with the plugin jstree [closed]
- Why the images on mi javascript are not found?
- dropdown does not work [closed]
- How do I cycle a JS function in WordPress? [closed]
- Styling parent based on a child element of a child in a WordPress page
- I have custom html mixed with inline javascript that I want added to my WordPress site, how do I achieve this?
- Sending GravityForms to custom HTML
- Processing javascript on wordpress
- How to use Javascript to get data from a WordPress form