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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With