Click here to Skip to main content
15,789,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As it says in the title, I get this error when I click the button to save the form data and then I try to recover it through the DNI, the fact is that after many changes I cannot find out why this omits the error.

more exactly this is what gives me the error

<pre>Uncaught ReferenceError: datos is not defined<br />
<br />
    guardar file:///f:/javascript recuperacion/EJER formulario/finalizar.js:41</pre>

Now I pass the code in the part where I think the error occurs, which is in the function called "save". As its name indicates, it saves the form data once it has been validated and with the retrieve function, prints them in another window by writing your ID.

function guardar() {

    document.getElementById('nombre').value = datos.nombre;
    document.getElementById('email').value = ;
    document.getElementById('edad').value = datos.edad;
    document.getElementById('etsex').value = datos.etsex;
    document.getElementById('tlf').value = datos.tlf;
    document.getElementById('estado_civil').value = datos.estado_civil;
    document.getElementById('seleccion').value = datos.seleccion;
    document.getElementById('txtdesc').value = datos.txtdesc;


    sessionStorage.setItem(guardaDNI, JSON.stringify(datos));

    datos = JSON.parse(sessionStorage.setItem(guardaDNI));

    function recuperar(){
        var guardaDNI = document.getElementById('guardaDNI').value;
            console.log("Pilla el item")
            var datos = sessionStorage.getItem(guardaDNI);
            window.alert("No hay datos del DNI : "+guardaDNI);
            document.getElementById('guardaDNI').value = "";

function recuperar(){
    var options = "top = 100, left = 100, width = 800px , height= 900px";
    var new_wind;
    new_wind ="","info", options);
    var nombre = document.getElementById("nombre").value;

    var id = document.getElementById("dni");
    var idmsg = "Tu DNI es: "+ document.getElementById("dni").value;

    var email = document.getElementById("email").value;
    var edad = document.getElementById("edad").value;

    var asx = document.getElementsByName("sexual");
    var sx;
    for(var i=0;i<asx.length;i++){
        if(asx[i].checked ){
            sx = asx[i].value;

    var tlf = document.getElementById("tlf").value;

    var reg = document.getElementById("estado_civil");
    var estado_civil = reg.options[reg.selectedIndex].value;

    var select = document.getElementsByName("seleccion");
    var sel = [];
    var seleccion = "";
    for(var i=0;i<select.length-1;i++){

    for(var i=0;i<sel.length;i++){
                seleccion += sel[i]+", ";
            }else if(i==sel.length){
                seleccion +=sel[i];
                seleccion += sel[i]+", ";
            }else if(i==sel.length-1){
                seleccion +=" y "+ sel[i]+".";

        seleccion +=" y "+ document.getElementById("extra").value+".";

    var descripcion = document.getElementById("txtdesc").value;
    new_wind.document.write("<html><head><link rel='stylesheet' href='style/style.css'></head><body><div id='result'><div id='flote'>"+
        "<h1>info:</h1><br><hr>Tu nombre es: "+nombre+"<br>"+idmsg+
        "<br>Tu email es: "+email+"<br>Tu sexo es: "+sx +"<br>Tu edad: "+edad+
        "<br>Tu teléfono de contacto: "+tlf+
        "<br>Su estado civil es: "+estado_civil+"<br>Acepto las cookies: "+seleccion+
        "<br>Por último lo que escribio es lo siguiente: "+descripcion+"</div></div></body></html>"

What I have tried:

I have tried to add parameters where it says "guardar" and nothing there is no way that it works
Updated 30-May-21 10:04am

1 solution

I making an assumption what line throws the error, but your code is assuming the following line successfully finds the item it's looking for. You don't make any attempt to validate getItem actually found an item.
var datos = sessionStorage.getItem(guardaDNI);
Well, getItem is failing to find an item, so it returns null. Now on the very next line, you're trying to use a property on an object that doesn't exist, or is null.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900