I would like to use the width I've given in CSS with a variable in Javascript. div.lilac {background-color:#5D0866; padding:0; text-indent:1em; width:70em;} I tried: <script type="text/javascript"> var boxbreite = div.lilac.width; </script> but that was probably silly ;-) I'm a very beginner. Could someone help please?
Easiest solution is to use some JavaScript library like jQuery and write something like: var $width = $('div.lilac').attr('width');
You could get the width from the element that is assigned the class, like this: <html> <head> <style> div.lilac { background-color:#5D0866; padding:0; text-indent:1em; width:700px; } </style> </head> <body> <div id="thingy" class="lilac"> I am the lilac </div> <script type="text/javascript"> alert(document.getElementById("thingy").offsetWidth); </script> </body> </html> Code (markup):