Categories
JavaScript Answers

How to display HTML code in text with sweet-alert and JavaScript?

Spread the love

Sometimes, we want to display HTML code in text with sweet-alert and JavaScript.

In this article, we’ll look at how to display HTML code in text with sweet-alert and JavaScript.

How to display HTML code in text with sweet-alert and JavaScript?

To display HTML code in text with sweet-alert and JavaScript, we can use the Swal.fire method.

For instance, we write

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

to add the sweet-alert 2 script tag.

Then we write

Swal.fire({
  title: "<i>Title</i>",
  html: "Test: <b>test</b>",
  confirmButtonText: "<u>OK</u>",
});

to call Swal.fire with an object with the title, html content, and the confirmButtonText content of the alert box.

Conclusion

To display HTML code in text with sweet-alert and JavaScript, we can use the Swal.fire method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *