Restoring cookie value and changing element style

I have a cookie as yab_uploadmode and it has a numeric value ranging from 1-4 to 4 divs named btn1, btn2, btn3 and btn4.

how can i get the cookie value and apply the style to that specific element without using any frameworks.

Many thanks.

Hello,

Shishant Todi

0


a source to share


1 answer


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

      

we will use the above function to get the 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 using window.onload property use addEvent function .

+1


a source







All Articles