-2

I need help with some elements for my website, to enable logged in users to be able to alter the colour or background of their logged in area. I am looking for the best way to quickly accomplish this.

I would like something similar to this:

onclick="document.getElementById('id1').style.color = 'red'"> Click Me!

Though it needs to be permanent and remain after logging back in.

1 Answers1

0

Here's a simple solution I made in jsfiddle. http://jsfiddle.net/tycyy15L/5/

 window.onload = function() {


var boxlength = document.getElementsByClassName("coloredBox").length;


for(i = 0; i < boxlength; i++){
document.getElementsByClassName("coloredBox")[i].onclick = function(){
document.body.style.backgroundColor = this.style.backgroundColor;
            }
    }

}

(see js fiddle for the rest) and cookie version.

If it needs to be permanent, you'll most likely need to use a database. If it's semi-permanent, you could use cookies like my fiddle does.

Starkey
  • 16
  • 3
  • Thank you, this does change the background colour. However when accessing another page the colour reverts as it is only applied to the page it is present on. Can you suggest how I can apply this to multiple pages? – Mark Corke May 13 '15 at 14:33
  • This section of the code if(document.cookie != undefined){ document.body.style.backgroundColor = document.cookie.substr(6); } should be put on all the pages you wan't the background to persist on. – Starkey May 13 '15 at 14:50
  • Currently, the cookie is not activating in Chrome, if I refresh the page the code is present on it resets, though it remains active in Internet Explorer. However, applying the code to additional pages is not currently working. Do you have a solution for this? Thanks again. – Mark Corke May 13 '15 at 15:33
  • I'm not sure I can help you with this. My fiddle works in the browsers I've tested with. The cookie I set is just an example. I'd research more if I were you. http://www.w3schools.com/js/js_cookies.asp. The cookie that you set should apply across the pages in your domain. If it doesn't, something is clearing it. – Starkey May 14 '15 at 12:25
  • Thanks for all your help Starkey. I really appreciate it! – Mark Corke May 14 '15 at 15:54