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

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


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?


To include a multiline value, just continue the text of the html attribute on the next line in your editor e.g.

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

will put the second hallo under the first


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.