I would like to pass an argument to another page but it turns out that it's undefined.
getAccount() returns a list of json. Firstly, I display those json objects one by one on HTML and when the user clicks each, the accDetail[i].accNo is set as local storage and will be passed to next page.
var accDetail=getAccounts();
for(var i =0;i<accDetail.length;i++) {
document.getElementById("demo").innerHTML+='<a href="accountdetails.html" onclick="getAcc('+accDetail[i].accNo+')">'+accDetail[i].accNo +'</a>'+' '+ accDetail[i].accType+' '+ accDetail[i].balance+'<br>';
}
This is the function to set the item as local storage.
function getAcc(item)
{
localStorage.setItem("accNo",item); }
It does not display the value I want, is the way I concatenate it wrong?
Your onclick
handler is going to be literally getAcc(accDetail[i].accNo)
(try viewing in Inspect Element), which won't work because accDetail
is not defined in the event handler. You need to change your Javascript so that it writes getAcc(0)
, getAcc(1)
, etc where 0, 1 are the different accNo
.
Here is a small example I wrote, hopefully you can extend it to solve your problem:
var accNo = [1, 2, 3, 4];
for(var i = 0;i < accNo.length; i++) {
var line = '<a href="accountdetails.html" onclick="getAcc(' + accNo[i] + ')">' + accNo[i] + '</a> <br>';
console.log(line);
document.getElementById("demo").innerHTML += line;
}
First Make sure your function getting correct value
Try Using window object instead
function getAcc(item)
{
localStorage.setItem("accNo",item);
}
Replace with
function getAcc(item)
{
console.log(item); // Please check you get item correct not undefined
window.accNo = item;
}And whereever you want just use window.accNo
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.