473,498 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

javascipt - the <br> is not working in my sort(), see script:

55 New Member
Expand|Select|Wrap|Line Numbers
  1. function sort() {
  2.     c.sort()
  3.     document.getElementById('display').innerHTML="";
  4.     document.getElementById('display').innerHTML=c +'<br>';
  5. }
  6.  
  7. // document.getElementById('display').innerHTML=c +'<br>';
  8. // can't get <br> tag to print each element on each line.
  9. // c is a array
how can i get each element printed on a new line?

ta
Aug 18 '07 #1
1 1527
si4
5 New Member
function sort() {
c.sort()
document.getElementById('display').innerHTML="";
document.getElementById('display').innerHTML=c +'<br>';
}

// document.getElementById('display').innerHTML=c +'<br>';
// can't get <br> tag to print each element on each line.
// c is a array

how can i get each element printed on a new line?

ta
When you call c, you print the whole array at once.
You need to walk through the elements with loop one by one.

Here sample code:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3.     <head>
  4.         <script type="text/javascript">
  5.             c = new Array( "Apple", "Lemon", "Banana", "Melon");
  6.             function sort() {
  7.                 c.sort()
  8.                 container = document.getElementById('display');
  9.                 container.innerHTML = "";
  10.                 for( var i=0; i < c.length; i++ ){
  11.                     //c.length = number of array elements in this case 4
  12.                     //the array looks like this:
  13.                     // c[0] = "Apple"; c[1] = "Lemon"; c[2] = "Banana"; c[3] = "Melon"
  14.                     // So we loop from 0 to 3( i < 4 )
  15.                     container.innerHTML = container.innerHTML + c[i] +'<br>';
  16.                 }
  17.             }
  18.             window.onload = sort;
  19.             // use this to call sort after DOM is loaded, otherwise container will have no properties, because document.getElementById('display') can't get the object, because it doesn't exist yet
  20.         </script>
  21.     </head>
  22.     <body>
  23.         <span id="display">Some text </span>
  24.     </body>
  25. </html>
  26.  
Hope this helps.

( Sorry for my English. It is not my native language. )
Aug 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
7834
by: fis | last post by:
Hi all, I've problem because there are needed break lines in my texts on the web site but i can't do it :( My pipeline looks like: XMS -> I18N -> XSLT -> HTML I have lot of texts in my...
6
12138
by: Lasse | last post by:
I have done this simple function, it seems to work as intended, to solve a problem i have had for a while. I couldnt find any sample around that was working for me. I would like to test it with...
7
19077
by: noor.rahman | last post by:
I have an XML file that stores data from an HTML form. I use XSL to display the data in HTML format. The data may have newline characters. However, XSL is not displaying the newlines properly in...
7
2727
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
2
2518
by: Winshent | last post by:
I have a multi line text in an admin page on my cms. I am trying to capture carriage returns as and replace them with <p></p> bfore the string gets written to the database. I have tried all...
1
3155
by: Winshent | last post by:
I have a multi line text in an admin page on my cms. I am trying to capture carriage returns as and replace them with <p></p> bfore the string gets written to the database. I have tried all...
2
19560
by: xhe | last post by:
I met a very headache problem in javascript, I think this might be difference between IE and NS / Safari. I have a text area <form> <textarea name='tex1' onkeyup='displayit();'></textarea>...
7
3592
by: Nathan Sokalski | last post by:
Something that I recently noticed in IE6 (I don't know whether it is true for other browsers or versions of IE) is that it renders <br/and <br></br> differently. With the <br/version, which is what...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.