views:

764

answers:

6

How do I include a newline in an HTML tag attribute?

For example:

<a href="somepage.html" onclick="javascript: foo('This is a multiline string.
This is the part after the newline.')">some link</a>


Edit: Sorry, bad example, what if the tag happened to not be in javascript, say:

<sometag someattr="This is a multiline string.
This is the part after the newline." />


Edit 2: Turns out the newline in the string wasn't my problem, it was the javascript function I was calling. FWIW, "&#10;" can be used for newline in an HTML attribute.

A: 

i'm not certain, but you can try \r or \n

javascript: foo('This is a multiline string.\rThis is the part after the newline.')

or

javascript: foo('This is a multiline string.\nThis is the part after the newline.')
Jason
What if it happened to not be within javascript?
Kip
are my examples not what you're looking for?
Jason
sorry, i picked a bad example, i updated the question
Kip
re: your edit - i don't think you can do that...
Jason
@Jason: You can.
AnthonyWJones
A: 

Usually, line breaks in HTML source code display what you intended in the result.

(Depends on the editor of course)

Suvesh Pratapa
A: 

Since it's in Javascript, you would use "\n" if inside double-quotes (not positive about single-quotes, I've been in PHP a lot lately.

Honestly, it's worth mentioning that you should use Events and a delegator instead of placing a javascript event directly on the element.

anonymous coward
+3  A: 

As a general rule newlines in attributes are preserved so your second example would work fine. Did you try it? Can you give a specific example where you are having problems with it?

As test take a look at this:-

<a href="somepage3.html" onclick="javascript: alert(this.getAttribute('thing'))" thing="This is a multiline string.
This is the part after the newline.">some link</a>

The alert include the newline in the attribute.

AnthonyWJones
oops, turns out the problem was actually the javascript function i was calling, not the newline in the tag.
Kip
+1  A: 

just put the the text of the html on the next line in your editor e.g.

<input type="submit" value="hallo
hallo">

will put the second hallo's under each other

edelwater
hmm... you're right. the specific problem i was encountering was with the newline inside the javascript, but i assumed it extended to any html attribute in general but i guess i was mistaken. :-/
Kip
+5  A: 

From what I remember about the HTML standard, character entities work in attributes, so this might work:

<sometag someattr="This is a multiline string.&#10;This is the part after the newline." />

I'm not sure if the "newline" you want ought to be &#10; (\n) or &#13;&#10; (\r\n), and I'm not sure if browsers will interpret it the way you want.

Why do you need it? What specific problem are you trying to solve by adding a newline in an HTML tag attribute?

Marius Gedminas
It does work. I was about to post the same answer.
Chuck
The XML standard for attribute normalization is: http://www.w3.org/TR/REC-xml/#AVNormalizeYou have a good memory :-)
Thomas L Holaday