[javascript] sweet-alert display HTML code in text

I am using sweet-alert plugin to display an alert. With a classical config (defaults), everything goes OK. But when I want to add a HTML tag into the TEXT, it display <b>...</b> without making it bold. After searching for the answer, it looks like I don't have the right search word...

How to make sweet alert display the text also with HTML code?

var hh = "<b>test</b>";
swal({
    title: "" + txt + "", 
    text: "Testno  sporocilo za objekt " + hh + "",  
    confirmButtonText: "V redu", 
    allowOutsideClick: "true" 
});

This question is related to javascript sweetalert

The answer is


All you have to do is enable the html variable to true.. I had same issue, all i had to do was html : true ,

    var hh = "<b>test</b>"; 
swal({
        title: "" + txt + "", 
        text: "Testno  sporocilo za objekt " + hh + "",  
        html: true,  
        confirmButtonText: "V redu", 
        allowOutsideClick: "true"  
});

Note: html : "Testno sporocilo za objekt " + hh + "",
may not work as html porperty is only use to active this feature by assign true / false value in the Sweetalert.
this html : "Testno sporocilo za objekt " + hh + "", is used in SweetAlert2


Use SweetAlert's html setting.

You can set output html direct to this option:

var hh = "<b>test</b>";
swal({
    title: "" + txt + "", 
    html: "Testno  sporocilo za objekt " + hh + "",  
    confirmButtonText: "V redu", 
    allowOutsideClick: "true" 
});

Or

swal({
    title: "" + txt + "", 
    html: "Testno  sporocilo za objekt <b>teste</b>",  
    confirmButtonText: "V redu", 
    allowOutsideClick: "true" 
});

As of 2018, the accepted answer is out-of-date:

Sweetalert is maintained, and you can solve the original question's issue with use of the content option.


I just applied the patch above and it starts working.

_x000D_
_x000D_
diff --git a/sweet-alert.js b/sweet-alert.js_x000D_
index ab6e1f1..d7eafaa 100755_x000D_
--- a/sweet-alert.js_x000D_
+++ b/sweet-alert.js_x000D_
@@ -200,7 +200,8 @@_x000D_
       confirmButtonColor: '#AEDEF4',_x000D_
       cancelButtonText: 'Cancel',_x000D_
       imageUrl: null,_x000D_
-      imageSize: null_x000D_
+      imageSize: null,_x000D_
+      html: false_x000D_
     };_x000D_
 _x000D_
     if (arguments[0] === undefined) {_x000D_
@@ -224,6 +225,7 @@_x000D_
           return false;_x000D_
         }_x000D_
 _x000D_
+        params.html               = arguments[0].html;_x000D_
         params.title              = arguments[0].title;_x000D_
         params.text               = arguments[0].text || params.text;_x000D_
         params.type               = arguments[0].type || params.type;_x000D_
@@ -477,11 +479,18 @@_x000D_
         $cancelBtn = modal.querySelector('button.cancel'),_x000D_
         $confirmBtn = modal.querySelector('button.confirm');_x000D_
 _x000D_
+      console.log(params.html);_x000D_
     // Title_x000D_
-    $title.innerHTML = escapeHtml(params.title).split("\n").join("<br>");_x000D_
+    if(params.html)_x000D_
+      $title.innerHTML = params.title.split("\n").join("<br>");_x000D_
+    else_x000D_
+      $title.innerHTML = escapeHtml(params.title).split("\n").join("<br>");_x000D_
 _x000D_
     // Text_x000D_
-    $text.innerHTML = escapeHtml(params.text || '').split("\n").join("<br>");_x000D_
+    if(params.html)_x000D_
+      $text.innerHTML = params.text.split("\n").join("<br>");_x000D_
+    else_x000D_
+      $text.innerHTML = escapeHtml(params.text || '').split("\n").join("<br>");_x000D_
     if (params.text) {_x000D_
       show($text);_x000D_
     }
_x000D_
_x000D_
_x000D_


Sweet alerts also has an 'html' option, set it to true.

var hh = "<b>test</b>";
swal({
    title: "" + txt + "", 
    html: true,
    text: "Testno  sporocilo za objekt " + hh + "",  
    confirmButtonText: "V redu", 
    allowOutsideClick: "true" 
});

There's sweet Alert version 1 and 2. Actual version 2 works with HTML nodes.

I have a Sweet Alert 2 with a data form that looks this way:

<script>
 var form = document.createElement("div");
      form.innerHTML = `
      <span id="tfHours">0</span> hours<br>
      <input style="width:90%;" type="range" name="tfHours" value=0 step=1 min=0 max=25
      onchange="window.changeHours(this.value)"
      oninput="window.changeHours(this.value)"
      ><br>
      <span id="tfMinutes">0</span> min<br>
      <input style="width:60%;" type="range" name="tfMinutes" value=0 step=5 min=0 max=60
      onchange="window.changeMinutes(this.value)"
      oninput="window.changeMinutes(this.value)"
      >`;

      swal({
        title: 'Request time to XXX',
        text: 'Select time to send / request',
        content: form,
        buttons: {
          cancel: "Cancel",
          catch: {
            text: "Create",
            value: 5,
          },
        }
      }).then((value) => {
        console.log(value);
      });

 window.changeHours = function (value){
   var tfHours = document.getElementById("tfHours");
   tfHours.innerHTML = value;
 }
 window.changeMinutes = function (value){
   var tfMinutes = document.getElementById("tfMinutes");
   tfMinutes.innerHTML = value;
 }

Have a go to the Codepen Example!


I was upgrading from old sweetalert and found out how to do it in the new Version (official Docs):

// this is a Node object    
var span = document.createElement("span");
span.innerHTML = "Testno  sporocilo za objekt <b>test</b>";

swal({
    title: "" + txt + "", 
    content: span,
    confirmButtonText: "V redu", 
    allowOutsideClick: "true" 
});

A feature to allow HTML for title and text parameters has been added with a recent merge into the master branch on GitHub https://github.com/t4t5/sweetalert/commit/9c3bcc5cb75e598d6faaa37353ecd84937770f3d

Simply use JSON configuration and set 'html' to true, eg:

swal({ html:true, title:'<i>TITLE</i>', text:'<b>TEXT</b>'});

This was merged less than a week ago and is hinted at in the README.md (html is set to false in one of the examples although not explicitly described) however it is not yet documented on the marketing page http://tristanedwards.me/sweetalert


I just struggled with this. I upgraded from sweetalert 1 -> 2. This library: https://sweetalert.js.org/guides/

The example from documentation "string" doesn't work as I expected. You just can't put it like this.

content: `my es6 string <strong>template</strong>` 

How I solved it:

const template = (`my es6 string <strong'>${variable}</strong>`);
content: {
      element: 'p',
      attributes: {
        innerHTML: `${template}`,
      },
    }

There is no documentation how to do this, it was pure trial and error, but at least seems to work.


I assume that </ is not accepted inside the string.

Try to escape the forward slash "/" by preceding it with a backward slash "\" for example:

var hh = "<b>test<\/b>";