简体   繁体   中英

How to pass javascript value to cookie

i'm trying to pass javascript value to cookie, but code passes only [object HTMLInputElement] value to cookie, what i'm doing wrong?

var M=document.getElementById('a');

//   counts <span> elements
var N=M.getElementsByTagName('Span');

var largo = N.length;


var myData = Array();
for (var i = 0; i < N.length; i++) {
    myData[i]=N[i].innerHTML;
}


function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1);
    if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}

function checkCookie() {
setCookie("chords", myDATA, 365);
}

this code gets all the chords from elements and I need to store all those chords into cookie

As far as I can tell, you're trying to show the span elements value in the cookie, instead of [object HTMLSpanElement]

So if you check my fiddle, it will show that it returns the value of the span elements inside the cookie.

JSFIDDLE

function checkCookie() 
{
    for (var i = 0; i < N.length; i++) 
    {
        myData[i] = N[i].innerHTML;
    }

    if (getCookie("chords"))
    {
        console.log(" COOKIE EXISTS ");
    }
    else
    {
       console.log(" COOKIE DOESN'T EXISTS ");
       setCookie("chords", myData, 365);  
    }
}

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