Shishant
Shishant

Reputation: 9294

retriving cookie value and changing element style

I have a cookie set as yab_uploadmode and it has numeric value ranging from 1-4 and 4 div elements named btn1, btn2, btn3 & btn4,

how can i retrieve the value of cookie and apply style to that particular element without use of any framework.

Thank you very much.

Regards,

Shishant Todi

Upvotes: 1

Views: 643

Answers (1)

Rafael
Rafael

Reputation: 18522

function getCookie(N){
   if(N=(new RegExp(';\\s*'+N+'=([^;]*)')).exec(';'+document.cookie+';'))
      return N[1]
}

we'll be using the above function to get cookie value.

window.onload=function(){
   var element, cookie = getCookie('yab_uploadmode');
   if(cookie && (element = document.getElementById('btn'+cookie))){
      //element.className = 'newClass'; // you can change the class...
      element.style.color='red'; // ... or a single property
   }
}

if you don't like to use the window.onload property, use the addEvent function instead.

Upvotes: 1

Related Questions