Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

doobz's avatar
Level 1

VueJS remove html tags from rendered text

I am needing to remove the opening and closing <p> tags from some rendered comment text. I pass the content to a component as a prop and i think that in doing so, it doesn't allow for the v-html directive to work correctly.

I need the content to render without the html tags

Here is where I am trying to render normally with v-html

 <textarea class="form-control comment-inline-edit" v-html="content" name="comment-inline-edit" cols="45" rows="3"></textarea>

And here is where I am passing the rendered content from the parent component

<CommentEdit v-show="isEditting" :content="comment.content.rendered" v-on:cancel="cancelEdit" />

Is there a VueJS way to do this other than using v-html?

0 likes
5 replies
topvillas's avatar

You could use a computed property that strips out the HTML tags and render that as the textarea content.

Sam T.'s avatar

You can try something like this:

computed: {
  	strippedHtml() {
        let regex = /(<([^>]+)>)/ig;
        
	return this.comment.content.rendered.replace(regex, "");
    }
}
2 likes
akamrah's avatar

@Sam T. Nice answer, I just converted this to a composable :

export function useStripHtml(html : string) {
    let regex = /(<([^>]+)>)/ig;
    return html.replace(regex, "");
}
justrusty's avatar

My preferred method is

stripTags(str) {
    let doc = new DOMParser().parseFromString(str, 'text/html');
    return doc.body.textContent || "";
}

Please or to participate in this conversation.