I wanted to know if it's possible to add HTML tags to JavaScript alert()
method, such as:
<b> <ul> <li>
etc.
Thanks for your help
One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen. Before this function can work, we must first call the showAlert() function. JavaScript functions are called in response to events.
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.
The alert() is a method of the window object.
The alert() method in JavaScript is used to display a virtual alert box. It is mostly used to give a warning message to the users. It displays an alert dialog box that consists of some specified message (which is optional) and an OK button. When the dialog box pops up, we have to click "OK" to proceed.
You can use all Unicode characters and the escape characters \n
and \t
. An example:
document.getElementById("test").onclick = function() { alert( 'This is an alert with basic formatting\n\n' + "\t• list item 1\n" + '\t• list item 2\n' + '\t• list item 3\n\n' + '▬▬▬▬▬▬▬▬▬ஜ۩۞۩ஜ▬▬▬▬▬▬▬▬▬\n\n' + 'Simple table\n\n' + 'Char\t| Result\n' + '\\n\t| line break\n' + '\\t\t| tab space' ); }
<!DOCTYPE html> <title>Alert formatting</title> <meta charset=utf-8> <button id=test>Click</button>
Result in Firefox:
You get the same look in almost all browsers.
alert() is a method of the window object that cannot interpret HTML tags
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