Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SweetAlert text formatting

I am using sweetAlert for dialog box display. Inside my dialog box, I have to display a large string with line breaks in between. My sample string is as follows:

var str="Task1Name          :    Success  :   statusCode\n 
         Task2NameLonger    :    Failed   :   statusCode\n"

and so on. So, basically, I want each of them in a new line and the spaces matched. When I use sweetalert dialog box line breaks are showing up properly but the text is aligned to the centre automatically and spacing is truncated. Can someone help me to manually set my alignment and spacing?

like image 837
Nagireddy Hanisha Avatar asked Jun 06 '17 06:06

Nagireddy Hanisha


People also ask

How do you customize Sweetalert?

Add buttons As you can see, the plugin adds an OK button to the alert. The button is fully customizable. You can even add new buttons to the alert message. Sometimes, you may need to handle button click events.

What is Sweetalert in JavaScript?

Sweet Alert is a way to customize alerts in your games and websites. It allows you to change from a standard JavaScript button. We can add buttons, change the color text, and even add additional alerts that change depending on user click.


1 Answers

Wrapping a string into <pre> tag could be a solution:

var str="Task1Name          :    Success  :   statusCode\n" +
        "Task2NameLonger    :    Failed   :   statusCode\n";
         
Swal.fire({
  html: '<pre>' + str + '</pre>',
  customClass: {
    popup: 'format-pre'
  }
});
.format-pre pre {
  background: #49483e;
  color: #f7f7f7;
  padding: 10px;
  font-size: 14px;
}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

PS. The original sweet alert plugin is unsupported, I suggest you using SweetAlert2 plugin.

Migration is simple, here's the migration guide: Migration from SweetAlert to SweetAlert2

like image 179
Limon Monte Avatar answered Sep 22 '22 08:09

Limon Monte