0

I want to send the data using sessionStorage to the other page, it works at first. However, when I call another function to make the dropdown list, only one function that works depends on the position.

I have tried to change the position of the function in function init(), and I have tried to check whether the sessionStorage value is sent or not using alert(sessionStorage.title); but when I call the selection function, the sessionStorage value is undefined.


function init(){

  alert (sessionStorage.title);
  extractDataReceived();
  selection();

  if (window.location != "enquiry.html"){
      sessionStorage.clear();
  }

  datatransfer = document.getElementById("datatransfer");
  datatransfer.onsubmit = storeDataReceived();

}

function storeDataReceived(){
  sessionStorage.title = document.getElementById("title").value;
}

function extractDataReceived(){
  if (sessionStorage.title != undefined){
    document.getElementById("subject").textContent = sessionStorage.title;
    }
}

function selection(){
    var select = document.getElementById("iselect");
    var options = ["(CAPSULE) Creatista Royale", "(CAPSULE) Lattissima Tactile", "(CAPSULE) Slique Limousine", "(CAPSULE) Ala Doby", "(CAPSULE) Essenza Minima", "(CAPSULE) Velour Retro",
                    "(BEAN-TO-CUP) De'Longhi Magnifica ESAM 4200.S", "(BEAN-TO-CUP) Melitta Caffeo SOLO and perfect milk", "(BEAN-TO-CUP) Gaggia Brera", "(BEAN-TO-CUP) Cuisinart One Cup Grind and Brew", "(BEAN-TO-CUP) Breville Barista Express",
                    "(MANUAL) The Barista", "(MANUAL) VGEBY Espresso Coffee Maker", "(MANUAL) Philips Seaco Poemia", "(MANUAL) Delonghi ECP35,31", "(MANUAL) Gemilai Italia"];


        for(var i = 0; i < options.length; i++ ){
            var opt = options[i];
            var ele = document.createElement("option");

            ele.textContent = opt;
            ele.value = opt;
            select.appendChild(ele);
        }

}

window.onload = init;

I expected that the sessionStorage value will be the title not undefined and I expected to have the dropdown list or select option to work.

Yaser Darzi
  • 1,480
  • 12
  • 24
Sun10
  • 1
  • Remove the `()` in `datatransfer.onsubmit = storeDataReceived();` – Andreas Nov 02 '19 at 08:47
  • Should be a duplicate of: [In JavaScript, does it make a difference if I call a function with parentheses?](https://stackoverflow.com/questions/3246928/in-javascript-does-it-make-a-difference-if-i-call-a-function-with-parentheses) – Andreas Nov 02 '19 at 08:51
  • It doesn't work for me, it still give me undefined for the session storage value. – Sun10 Nov 02 '19 at 09:09

0 Answers0