kampeao
kampeao

Reputation: 21

javascript localstorage

So, here's my code:

localStorage.setItem("nhac",for(p=0;p<opener.document.getElementsByName('link').length;p++){opener.document.getElementsByName('link').item(p).style.backgroundColor})

I'm so confused i can't think anymore xD, I've been trying all day long to make this work, i can't even explain it,sorry. I have a popup which i opened from the main page. From the pop up window, im retrieving the elements from which i want to save the background-color so, by saving it in localstorage, it will always remain like that. I've done others that work well, like this

localStorage.setItem("color_chosen2", opener.document.getElementById('header').style.color);

I don't know why it doesn't work, can anyone help me? It gives me some kind of error( the first one)

thanks

Edit:(sorry, I was in a hurry when i posted this) The error it gives is "SyntaxError: syntax error" (I'm using firefox, by the way) Using the javascript console, this code

for(p=0;p<opener.document.getElementsByName('link').length;p++){opener.document.getElementsByName('link').item(p).style.backgroundColor}

returns the background color of the elements perfectly, but when I try to use it as the value in localStorage , it gives an error. And no, there's no problem with the syntax localStorage.setItem, it's how it is, thanks.

Edit2: if you need to see how it works better, here's my website. the pop-up opens from the link in the bottom center.

Upvotes: 1

Views: 393

Answers (2)

kampeao
kampeao

Reputation: 21

Well, noone really effectively helped me, but at least 2 people tried to help, so thank you. Anyway, i don't know how, I had an idea and the code works now...to anyone interested:

if(localStorage.getItem("color_chosen3")){
 for(p=0;p<document.getElementsByName('link').length;p++){document.getElementsByName('link').item(p).style.backgroundColor=localStorage.getItem("color_chosen3")}}}

Problem solved, question closed...whatever. thanks

Upvotes: 1

uadnal
uadnal

Reputation: 11425

You are trying to set a for loop into a setter which accepts a String. You must set each of the styles individually and with a unique key value.

Also, I don't think item is a method...

Try this...

items = opener.document.getElementsByName('link');
for ( p = 0; p < items.length; p++ ){
     localStorage.setItem( p + 'nhac', items[p].style.backgroundColor );
}

Upvotes: 1

Related Questions