0

I am wondering how do i put a "next line" for a variable that has multiple items in Javascript. Do i put \ or \n or \r\n or an html line break in this line?

This is the code:

 var html = "Title : " + data[i].title + "   description \r\n  : " + data[i].description +  "\n"; 



 var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.rect(20,(rectSize+100)*i,250,80);
           ctx.fillText(html, 20,(rectSize+120)*i);
ctx.stroke();

I am trying to put 2 items in a variable that is going through a loop and i want the items to be in different lines. Or do i have to assign different variables to each item?

Benyaman
  • 451
  • 2
  • 10
  • 25

1 Answers1

0

Unless your HTML variable is populating a pre element, HTML documents will ignore the \r and \n escape characters anyway. You can instead use the br element:

var html = "..." + ... "... <br>" + ... ;
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • 2
    @AmitJoki `
    ` is fine as it is if it's HTML and not XHTML.
    – James Donnelly Jul 04 '14 at 15:15
  • I tried putting in
    but it is just displaying
    as text tho in the html, its not breaking the line
    – Benyaman Jul 04 '14 at 15:17
  • @Clarklight how are you adding it? You need to use `innerHTML`, not `innerText`, for instance. – James Donnelly Jul 04 '14 at 15:19
  • I am adding it in ctx.filltext – Benyaman Jul 04 '14 at 15:20
  • @Clarklight that completely changes the context of the question. You should specify things like this at the beginning. My answer is now worthless and this question is now a duplicate of: http://stackoverflow.com/questions/5026961/html5-canvas-ctx-filltext-wont-do-line-breaks – James Donnelly Jul 04 '14 at 15:21
  • ooo sorry i didnt know that, this is the first time i am working on this – Benyaman Jul 04 '14 at 15:22
  • I am just trying to display a few texts that through a loop, is ctx the only way to do so? Or is there any other ways? I am just trying to loop through some data, and put each set of data into a rectangle box for display. – Benyaman Jul 04 '14 at 15:27
  • @Clarklight I don't know enough about developing with Canvas to be able to help I'm afraid. I believe the question I've linked you to should be able to answer your question though. – James Donnelly Jul 04 '14 at 15:27