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.

7 Answers 7

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?

6
  • 3
    The XML standard for attribute normalization is: w3.org/TR/REC-xml/#AVNormalize You have a good memory :-) Commented Jun 11, 2009 at 22:11
  • 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 />
    – Bao
    Commented Aug 15, 2013 at 15:29
  • 1
    @Baowen: you forgot to terminate your title attribute with a ". Commented Aug 19, 2013 at 16:08
  • @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.
    – Bao
    Commented Aug 20, 2013 at 16:38
  • @Bao newline in attributes aren't the same as newlines in the tool tip. A literally newline in HTML doesn't render as a newline Commented Jun 6, 2019 at 12:44
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

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. :-/
    – Kip
    Commented Jun 11, 2009 at 21:57
  • 1
    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 ?
    – Jcyrss
    Commented May 11, 2022 at 1:57
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.

2
  • oops, turns out the problem was actually the javascript function i was calling, not the newline in the tag.
    – Kip
    Commented Jun 11, 2009 at 22:06
  • Just to clarify, newlines in attribute values ARE preserved, Commented Feb 1, 2015 at 4:40
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.

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.')
2
  • What if it happened to not be within javascript?
    – Kip
    Commented Jun 11, 2009 at 21:40
  • are my examples not what you're looking for?
    – Jason
    Commented Jun 11, 2009 at 21:41
0

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

(Depends on the editor of course)

-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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.