Alex Rivera | Logout

How can I use a multiline value for an HTML tag attribute? (i.e. how do I escape newline?)

Asked 2009-06-11T21:37:28.987
25

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.

Edit
Report

2 Answers

28

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?

answered 2009-06-11T22:04:41.837
0

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

(Depends on the editor of course)

answered 2009-06-11T21:39:47.497

Your Answer