How can I format the text in a JavaScript alert box? I need a word in the text to be underlined.
The standard alert box in JavaScript does not provide the option to apply CSS. To style your alert box, you need to create a custom one first. The custom alert box will be created using jQuery and styles will be applied to CSS.
The Window alert() method is used to display an alert box. It displays a specified message along with an OK button and is generally used to make sure that the information comes through the user. It returns a string which represents the text to display in the alert box.
It's not possible to edit a JavaScript alert box title due to security issues. Still, to edit an alert box to work it all web browsers, use custom JavaScript Modal Dialogs or jQuery plugins.
You can use only:
\b = Backspace \f = Form feed \n = New line \r = Carriage return \t = tab \000 = octal character \x00 = hexadecimal character \u0000 = hexadecimal unicode character
So, you can insert different ASCII characters, but not format them (like italic or bold).
EDIT I also have to mention, that in fact alert
acts like toString
conversion, so including tags and/or styles is not possible.
Underline a string using unicode character '\u0332'
, called a COMBINING LOW LINE
function underline(s) { var arr = s.split(''); s = arr.join('\u0332'); if (s) s = s + '\u0332'; return s; } var str = underline('hello world'); // "h̲e̲l̲l̲o̲ ̲w̲o̲r̲l̲d̲" alert(str);
This method is not guaranteed to produce a smooth underline.
Example of confirm()
on Firefox 59:
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