Javascript write to text file Code Answer’s
Writing a text file is a very common task in web development. Many times, you may want to write a text file dynamically. In this tutorial, we are going to see how to write to a text file using Javascript. Write to file is a javascript function that can be used to write text to a text file. This function is very useful when you want to save something in the browser's hard drive.
write to file js
// write to js file
const fs = require('fs')
const content = 'this is what i want to write to file'
fs.writeFile('/Users/joe/test.txt', content, err => {
if (err) {
console.error(err)
return
}
//file written successfully
})
javascript write to text file.
function WriteToFile(passForm) {
set fso = CreateObject("Scripting.FileSystemObject");
set s = fso.CreateTextFile("<your Path>/filename.txt", True);
var firstName = document.getElementById('FirstName');
var lastName = document.getElementById('lastName');
s.writeline("First Name :" + FirstName);
s.writeline("Last Name :" + lastName);
s.writeline("-----------------------------");
s.Close();
}
// HTML
<form onSubmit="WriteToFile(this)">
<label>Type your first name:</label>
<input type="text" name="FirstName" id="firstName" size="20">
<label>Type your last name: </abel>
<input type="text" name="LastName" id="lastName" size="20">
<input type="submit" value="submit">
</form>
In this tutorial, we will learn how to write data in text format using javascript. It is very easy to write data in text format using javascript. We can create a file using fs module of Nodejs. The fs module provides us the functions like append file(), close(), createWriteStream() and so on. We can use these functions to create or open a file and write our data into it.