1

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.

3
  • "some kind of error" doesn't really help us Commented Jul 2, 2012 at 20:31
  • "SyntaxError: syntax error" while using firefox javascript console Commented Jul 2, 2012 at 23:34
  • Still haven't made any progress, does anyone else have any idea why it gives an error? Commented Jul 3, 2012 at 8:26

2 Answers 2

1

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 );
}
Sign up to request clarification or add additional context in comments.

1 Comment

the problem isn't with the localStorage.setItem("key",value). Thank you anyhow :)
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.