skip to Main Content

for example I have the description variable defined as follows in nextjs

description={storeDetail?.translation?.description}

and as output description it shows me this <p>Tenis</p> ,However I would like to display Tenis simply without the Html tags.

thank you for helping me!

2

Answers


  1. You can achieve this by creating a dummy HTML div using createElement method and then just fetch the innerText of that div and update the value of your variable

    For Example

    const trimHTML = (content) => {
      // Create temporary div element
      var element = document.createElement('div')
    
      // Assign the content as innerHTML for the element
      element.innerHTML = content
    
      // Return the text
      return (element.innerText || element.textContent)
    }
    
    // Output: Tenis
    console.log(trimHTML('<p>Tenis</p>'));
    Login or Signup to reply.
  2. The simplest way to do this is to use dangerouslySetInnerHTML

        <p dangerouslySetInnerHTML={{ storeDetail?.translation?.description }}/>
    

    Otherwise, there are libraries you can use. A good example is react-html-parser

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search