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

7

24

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.

Leucopenia answered 11/6, 2009 at 21:37 Comment(0)
Z
27

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?

Zhao answered 11/6, 2009 at 22:4 Comment(6)
The XML standard for attribute normalization is: w3.org/TR/REC-xml/#AVNormalize You have a good memory :-)Roslynrosmarin
It's not working in my case. Why? <input type="text" name="email" value="Enter email here" title="First line sentence.&#10;Second line sentence.><br />Shaeffer
@Baowen: you forgot to terminate your title attribute with a ".Zhao
@MariusGedminas Hi thanks, that too. But even after adding the ", it's still not working. I am trying to display those texts in a tooltip via jQuery tooltip function.Shaeffer
@Shaeffer newline in attributes aren't the same as newlines in the tool tip. A literally newline in HTML doesn't render as a newlinePinfish
Great to find this here (and not at first there w3.org/TR/REC-xml/#AVNormalize). This is an important feature for MediaWiki extensions which often pipe arguments through attributes, for example <youtube description"first paragraph&#10;second paragraph">...Shaunna
T
20

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="hallo
hallo"> 

will put the second hallo under the first

Tantra answered 11/6, 2009 at 21:52 Comment(2)
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. :-/Leucopenia
What if there are some blank lines in the value? like <iframe srcdoc='....'>. I need put the whole html in the value string, I have to remove all the blank lines of it ?Distance
B
6

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.

Burnout answered 11/6, 2009 at 21:49 Comment(2)
oops, turns out the problem was actually the javascript function i was calling, not the newline in the tag.Leucopenia
Just to clarify, newlines in attribute values ARE preserved,Phillis
S
2
<a href="somepage.html" onclick="javascript: foo('This is a multiline string. \
This is the part after the newline.')">some link</a>

Javascript needs a backslash at the end of the new line in a string.

Sprint answered 29/12, 2011 at 5:42 Comment(0)
G
0

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.')
Gottuard answered 11/6, 2009 at 21:39 Comment(2)
What if it happened to not be within javascript?Leucopenia
are my examples not what you're looking for?Gottuard
M
0

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

(Depends on the editor of course)

Moia answered 11/6, 2009 at 21:39 Comment(0)
O
-2

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.

Orientate answered 11/6, 2009 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.