Save JSON as a file to download it in .txt - Javascript

0

I have this code to first reflect a form in LocalStorage and then pass it to JSON, but I need to download a file with certain data to a text file.

I leave the example:

 function getAllForm(){


    var formID = document.getElementById('form').elements;
    for(var i = 0; i<= formID.length - 1; i++){

        if(formID[i].id == 'nc_' + pCO || 'tc_' + pCO || 'op_' + pCO || 'obl_' + pCO){

            console.log(form[i].value);
            localStorage.setItem(form[i].id, JSON.stringify(form[i].value));
        }

        var jsonOnText = JSON.stringify(form[i].value);
        var hiddenElement = document.createElement('a');
        hiddenElement.href = 'data:attachment/text,' + encodeURI(jsonOnText);
        hiddenElement.target = '_blank';
        hiddenElement.download = 'jsonOnText.txt';
        hiddenElement.click();

        //return jsonFields; 
    }


    //IMPRIMO EL RESULTADO DEL LOCALSTORAGE EN JSON EN CONSOLA PARA VER
    console.log(localStorage); 
}

I thanked the help, as I said I need to download certain data to a text file.

* I already do it, it is downloaded BUT it only brings me the first data or value of the form, example the form has: name, lastname, address, email, age) only brings me the value of name in this case, I need that Save me all or just name, lastname and address.

THANKS!

    
asked by ortizalfano 17.09.2018 в 19:22
source

0 answers