Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Save html email input to json file

Do some know how i can save the mail after it is writing in a input in a json file.

 <form id="myform" action="" method="post">
                <input id="newsletter-input" placeholder="Enter email for newsletter" type="text">
 </form>

4 Answers

I am making a project by my own is not from courses

Steven Parker
Steven Parker
242,191 Points

It's possible that the question needs a more detailed description. As it is, it doesn't make much sense.

This code only deals with an email address, and not any actual email message content. There is nothing here that can be converted into JSON.

Even if you had a message, you would need to define a specific schema for converting email content into JSON. Typical email message contents do not have a structure that would naturally be convertible into keyword/value pairs as required for JSON.

Also, web page code is "sandboxed" by the browser and prohibited from creating or writing to files as a security measure.

But it should be a method to save the email after i put it in the console

var form = document.getElementById("myform");
form.onsubmit = function(evt){
  evt.preventDefault();
  console.log(form.myemail.value);
};
Steven Parker
Steven Parker
242,191 Points

Similar to the HTML, this code only deals with an email address, and not any actual email message content. Do you want to save just the address? And you still need a key/value pair to convert it to JSON.

Perhaps more importantly, this code seems to be intended for a web page. Web page code is "sandboxed" by the browser and prohibited from creating or writing to files.

I was asking for this just to save the e-mail in console:

var form = document.getElementById("myform");
form.onsubmit = function(evt){
  evt.preventDefault();
  console.log(form.myemail.value);
  var jobj={email: form.myemail.value}; //JSON object
  console.log(JSON.stringify(jobj));

};
Steven Parker
Steven Parker
242,191 Points

But the point is that you can't write files from the browser.