views:

205

answers:

1

Hello,

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

+1  A: 
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.

Rafael
i have added the code to my script header and change element.className to element.style.color= '#333'; i am not getting any errors in firebug but the color is not changing when the page is loaded
Shishant
i used onload=getCookie('yab_uploadmode') and also tried wrapping the 2nd part ie var element.....newclass in a function btn and tried onload=btn(); but no success
Shishant
just edited my post. Try with the new code
Rafael
tried but no success, i even tried to log the value of element in firebug but the value is not displaying in firebugwindow.onload=function(){ var element, cookie = getCookie('yab_uploadmode'); if(cookie // you can change the class... element.style.color='#333'; // ... or a single property console.log(element); }}
Shishant
Rafael
<div id="btn1" name="btn1"> my divs has both name and id
Shishant
NAME attribute for DIV isn't defined in HTML spec. But that isn't the problem. Have no idea what could cause my code not to work. Tested it on my sample page and everything was working fine. Make sure, there are no other scripts that use window.onload, because this could overwrite my code. Open the Error Console in your browser and check for any reports.If this isn't the case, please prepare a test page.
Rafael