Aditya Ponkshe
Aditya Ponkshe

Reputation: 3900

How can I write data in a single line on text file using javascript

I am trying to write data in a .txt file using JavaScript function. My function :-

function WriteToFile()
{
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var s = fso.CreateTextFile("test.txt", true);
    for(var j=1;j<9;j++)
    {
        if(document.getElementById('chk'+j).checked)
        {
            s.WriteLine('  ');
            s.WriteLine(document.getElementById('item'+j).innerText);
            s.WriteLine(',');
            s.WriteLine(document.getElementById('txtBx'+j).value);
            s.WriteLine(',');
            s.WriteLine(document.getElementById('txtBxx'+j).value);
            s.WriteLine(';');
        }
    }
    alert("written");
    s.Close();
}

Now the problem is that data being written on a new line. (maybe because i am using s.WriteLine ?) I want all the data to be written in a single line. how can i achieve this?

to elaborate more, my current output looks like this

abc
,
1
,
cdf
;

def
,
3
,
ehi
;

I want it like this-

abc,1,cdf;def,3,ehi;

Upvotes: 0

Views: 1770

Answers (2)

Barmar
Barmar

Reputation: 782785

Use s.write() instead of s.writeLine(). As the name implies, the second function is for writing whole lines, and it adds a newline after it.

Upvotes: 2

Doink
Doink

Reputation: 1162

Just add everything to a variable then write the line.

var st = document.getElementById('item'+j).innerText;
st += ','
st += document.getElementById('txtBx'+j).value;
s.WriteLine(st);

Upvotes: 0

Related Questions