By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,441 Members | 1,261 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,441 IT Pros & Developers. It's quick & easy.

line breaks in alert box from html script

P: 3
I'm building an HTML page with javascript. All is fine in the document.write until I want to create an alert box from an onClick on the page.
Here's the script(part) so far:"<td><a href onclick=alert('Some&nbsp;list&nbsp;here')>The list</a></td>".
This brings back alert box with - Some list here - OK but I want a line break where the second &nbsp; is. I've tried all the \n \r <br> and <p> and %0A combinations I can think of.
Any ideas please. Remember that " doesnt work because the script is aleady within double quotes. ps I cant get a var to work from inside the script either !?
Oct 14 '11 #1

✓ answered by Humphrey Shires

Many thanks gits but that doesn't work, the problem is how html reads the script passed over from js - but I've now sussed it (after a few hours) -- the \n needs an escape in front of it ie \\n.
Posted for others is the part of document.write that applies (note the quotes):
"<table><tr><td><a href onclick=alert('Some&nbsp;list\\nhere')>The list</a></td></tr></table>".
Try this in a table created from js and click on the words 'The list'
Thnx to all who looked at it for me

Share this Question
Share on Google+
4 Replies


gits
Expert Mod 5K+
P: 5,390
in an alert \n should work:

Expand|Select|Wrap|Line Numbers
  1. alert('foo\nbar');
and of course can you use a variable, JavaScript is a programming language so you would be lost without that possibility.
Oct 14 '11 #2

P: 3
Many thanks gits but that doesn't work, the problem is how html reads the script passed over from js - but I've now sussed it (after a few hours) -- the \n needs an escape in front of it ie \\n.
Posted for others is the part of document.write that applies (note the quotes):
"<table><tr><td><a href onclick=alert('Some&nbsp;list\\nhere')>The list</a></td></tr></table>".
Try this in a table created from js and click on the words 'The list'
Thnx to all who looked at it for me
Oct 14 '11 #3

gits
Expert Mod 5K+
P: 5,390
basically the following is not really valid:

Expand|Select|Wrap|Line Numbers
  1. <a href onclick=alert('Some&nbsp;list\\nhere')>
and should be:

Expand|Select|Wrap|Line Numbers
  1. <a href onclick="alert('Some&nbsp;list\nhere')">
is the shown code a snippet in a php script? in case it is then in fact it will turn out as:

Expand|Select|Wrap|Line Numbers
  1. <a href onclick=alert('Some&nbsp;list\nhere')>
later when the page is rendered. but attribute values should be enclosed in (double-) quotes
Oct 14 '11 #4

P: 3
Hi gits, I've now replaced the text(between the 's in the alert box) with a variable. Had problem getting this to work until I entered the variables in the <script> section of the page <head>. I couldn't use double quotes in your example because all this snippet was inside a set of doubles to produce a row of a table. The project is for in-house so whatever works without bringing down the ceiling should be OK. The \\n works in the text variable and (bonus) &nbsp; is not needed: eg var strText = 'amounts\\n\\n1st\\nDue - $120.37'
Once again many thanks
Oct 14 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.