0

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;
1
  • 2
    try d.write and see if that works Commented Jul 5, 2013 at 9:13

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

worked, thanks a lot. i suspected s.writeline is the culprit but didn't know the alternative.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.