I have a array called 'data' which is used globally as it is being used in other functions. Each data has array of values like
data= "United States,Tennessee,Anderson";
Could some one please help me how put these comma separated values in a table. I was trying this in javascript:


data=new Array();

function display(){

//window.open(_blank,'myWindow');

document.write("<table border='1'>");
for(var i=0;i<data.length;i++)
{
document.write("<tr>");
data[i]=new Array();

for(var j=0;j<data[i].length;j++)
{
document.write("td>"+data[i][j]+"</td>");
}
document.write("</tr>");
}
document.write("</table>");
}

Doesn't look like you understand programming... Also, I don't really completely understand what you mean... You mean, you want to split the string and then put it in a table? An example below is to write out the page content with splitting string. However, it does not work with already loaded page because it will overwritten the whole page content.

<script type="text/javascript">
// take the string (str) in as argument
function display(str) {
  // str could be "US, Blah, Blah2, ..."
  var strArr = str.split(/\s*,\s*/)  // split using space_or_no_space comma space_or_no_space

  document.write("<table border='1'>")
  for(var i=0;i<strArr.length;i++) {  // iterate through each split string
    document.write("<tr>")
    document.write("<td>"+strArr[i]+"</td>")
    document.writeh("</tr>")
  }
  document.write("</table>");
}
</script>

Hello Taywin,

Thanks for the reply.
My str String is an array. I have tried the code which you posted but it is giving an error as 'str undefined'. My code is like this:

str =new Array();
function display(str) {

// str could be "US, Blah, Blah2, ..."
var strArr = str.split(/\s*,\s*/);
document.write("<table border='1'>")
for(var i=0;i<strArr.length;i++) { // iterate through each split string
document.write("<tr>")
document.write("<td>"+strArr[i]+"</td>")
document.writeh("</tr>")
}
document.write("</table>");

}

That is the reason I am finding it difficult to solve the problem. Could you please help me with this issue. :(

Your str is an array of what??? Another string in there? The function I gave you is taking a 'string' type argument, not an 'array' type... Maybe I should give more example about how to use it...

function display(str) {
 ...
}

var str1 = "US, Blah,Blah2,  OK"
var strArr = new Array()
strArr[0] = "US, Blah, Boo"
strArr[1] = "OK, try again, huh?"
strArr[2] = "More, and more,and and more"
// after this declaration, each variable will be
// str1 -> "US, Blah,Blah2,  OK"
// strArr -> ["US, Blah, Boo", "OK, try again, huh?", "More, and more,and and more"]

display(str1)  // <- how to call the function using a string argument
for (var i=0; i<strArr.length; i++) { // <- how to call the function while iterating an array
  display (strArr[i])
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.