简体   繁体   中英

Pass Parameter From OnClick Event from one Javascript Function to Another

I'm trying to pass a value from a hyperlink click from one JS function to another. In this context, I need the hyperlink text, which is a key from local storage. I need to pass that to a different html/JS script to access this key from local storage there. I'm having a hell of a time accomplishing this. The last console.log(); statement in this script returns "Link names: undefined"

 myApp.onPageInit("saved_locations", function(page) { var fragment = document.createDocumentFragment(); var parent = document.getElementById("saved"); var node; // iterate localStorage for (var i = 0; i < localStorage.length; i++) { // set iteration key name var key = localStorage.key(i); // use key name to retrieve the corresponding value var value = localStorage.getItem(key); // console.log the iteration key and value console.log("Key: " + key + ", Value: " + value); let node = document.createElement("div"); let a = document.createElement("a"); a.className = "link"; a.textContent = key; a.style.color = "blue"; a.href = "map_G.html"; node.appendChild(a); fragment.appendChild(node); } parent.appendChild(fragment); var myForm = document.getElementById("enter_location"); myForm.addEventListener("submit", function saveSearchLocation(event) { //event.preventDefault(); var lat = document.getElementById("Latitude").value; var lon = document.getElementById("Longitude").value; var locationStr = document.getElementById("Location").value; //Save location parameters to local storage savedLocationParams = [lat, lon, locationStr]; window.localStorage.setItem( locationStr, JSON.stringify(savedLocationParams) ); }); for (var i in document.getElementsByClassName("link")) { var link = document.getElementsByClassName("link")[i]; link.onclick = function(e) { linkNames = e.srcElement.attributes.textContent; console.log("Link names: " + linkNames); }; } }); 
 <body> <div class="pages"> <div data-page="saved_locations" id="saved" class="page navbar-through no- toolbar" align="center"> <h2><br /><u>Enter A Location<br /><br /></u></h2> <form id="enter_location"> Latitude: <input type="text" id="Latitude" value=""><br> Longitude: <input type="text" id="Longitude" value=""><br> Location: <input type="text" id="Location" value=""><br> <input type="submit" value="Submit"> </form> <h2><u>Saved Locations</u></h2> </div> </div> </body> 

Since the link name doesn't change, just define the onclick function while you still have the key handy.

let a = document.createElement("a");
a.className = "link";
a.textContent = key;
a.style.color = "blue";
a.href = "map_G.html";

a.onclick = function(e) {
  console.log("Link names: " + key);
};

node.appendChild(a);

At the bottom of this question is an MCVE of that solution.

Once you have it in the onclick, you could set another localstorage key that won't ever be a text of the link, something like "hambone_key" and set its value to the key you need to save, and then you can read "hambone_key" when you load up the page and get the key that way. Eg,:

a.onclick = function(e) {
  console.log("Link names: " + key);
  localStorage["hambone_key"] = key;
};

and then on page load:

var saved_key = localStorage.getItem("hambone_key");
if (saved_key === null) {
    // there is no saved key
} else { 
    // there is a saved key
    var important_value = localStorage.getItem(saved_key);

    // do stuff with important_value here
    // ....
}

So, in the context of the code you have provided, it looks like this:

myApp.onPageInit("saved_locations", function(page) {
  var fragment = document.createDocumentFragment();
  var parent = document.getElementById("saved");
  var node;
  // iterate localStorage
  for (var i = 0; i < localStorage.length; i++) {
    // set iteration key name
    var key = localStorage.key(i);

    // use key name to retrieve the corresponding value
    var value = localStorage.getItem(key);

    // console.log the iteration key and value
    console.log("Key: " + key + ", Value: " + value);

    let node = document.createElement("div");
    let a = document.createElement("a");
    a.className = "link";
    a.textContent = key;
    a.style.color = "blue";
    a.href = "map_G.html";

    a.onclick = function(e) {
      console.log("Link names: " + key);
      localStorage["hambone_key"] = key;
    };

    node.appendChild(a);

    fragment.appendChild(node);
  }

  parent.appendChild(fragment);

  var myForm = document.getElementById("enter_location");

  myForm.addEventListener("submit", function saveSearchLocation(event) {
    //event.preventDefault();

    var lat = document.getElementById("Latitude").value;
    var lon = document.getElementById("Longitude").value;
    var locationStr = document.getElementById("Location").value;

    //Save location parameters to local storage
    savedLocationParams = [lat, lon, locationStr];
    window.localStorage.setItem(
      locationStr,
      JSON.stringify(savedLocationParams)
    );
  });
});

And here is the MCVE of getting the key in the onclick function. The below code does not use localStorage here because it is forbidden in StackOverflow snippets:

 var fragment = document.createDocumentFragment(); var parent = document.getElementById("saved"); var node; var fakeLocalStorage = { "key0": "value0", "key1": "value1", "key2": "value2" }; // iterate localStorage //for (var i = 0; i < localStorage.length; i++) { for (var i = 0; i < 3; i++) { // set iteration key name //var key = localStorage.key(i); // use key name to retrieve the corresponding value //var value = localStorage[key]; // set iteration key name let key = Object.keys(fakeLocalStorage)[i]; // use key name to retrieve the corresponding value let value = fakeLocalStorage[key]; // console.log the iteration key and value console.log("Key: " + key + ", Value: " + value); let node = document.createElement("div"); let a = document.createElement("a"); a.className = "link"; a.textContent = key; a.style.color = "blue"; a.href = "javascript:return false;"; a.onclick = function(e) { console.log("Link names: " + key); }; node.appendChild(a); fragment.appendChild(node); } parent.appendChild(fragment); var myForm = document.getElementById("enter_location"); myForm.addEventListener("submit", function saveSearchLocation(event) { //event.preventDefault(); var lat = document.getElementById("Latitude").value; var lon = document.getElementById("Longitude").value; var locationStr = document.getElementById("Location").value; //Save location parameters to local storage savedLocationParams = [lat, lon, locationStr]; window.localStorage.setItem( locationStr, JSON.stringify(savedLocationParams) ); }); 
 <body> <div class="pages"> <div data-page="saved_locations" id="saved" class="page navbar-through no- toolbar" align="center"> <h2><br /><u>Enter A Location<br /><br /></u></h2> <form id="enter_location"> Latitude: <input type="text" id="Latitude" value=""> <br /> Longitude: <input type="text" id="Longitude" value=""> <br /> Location: <input type="text" id="Location" value=""> <br /> <input type="submit" value="Submit"> </form> <h2><u>Saved Locations</u></h2> </div> </div><br /><br /><br /><br /><br /><br /><br /> </body> 

I found a simple solution with JQuery:

$(a).click(function(e) {
          var txt = $(e.target).text();
          console.log("Link: " + txt)
          });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM