Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add css box-shadow using .style JavaScript property

I've got the following JavaScript snippet:

document.getElementById("imgA").style.box-shadow = "0 0 5px #999999";

The hyphen in box-shadow is causing the JavaScript engine to throw an invalid assignment exception (in Firefox). Doing "box-shadow" or 'box-shadow' doesn't work. Is there a good way around this without using jquery's .css() method?

like image 423
Escher Avatar asked Sep 17 '15 10:09

Escher


People also ask

Which CSS property is used to add shadow to an element?

The box-shadow CSS property adds shadow effects around an element's frame. You can set multiple effects separated by commas.

How do you add a box shadow to all sides in CSS?

Like in other comments set first two values to 0px in order to have even shadow on all sides.


4 Answers

You can use style["boxShadow"] or style.boxShadow.

document.getElementById("foo").style["boxShadow"] = "0 0 5px #999999";
<div id="foo">12123123</div>
like image 65
Joel Almeida Avatar answered Oct 29 '22 04:10

Joel Almeida


CSS properties with a - are represented in camelCase in Javascript objects. So you dont need a hyphen - just write boxShadow

document.getElementById("shadow").style["boxShadow"] = "0 0 5px #999999";
<div id="shadow">Tushar </div>
like image 36
Tushar Avatar answered Oct 29 '22 03:10

Tushar


Use boxShadow

document.getElementById("imgA").style.boxShadow = "0 0 5px #999999";
like image 21
Gabriele Petrioli Avatar answered Oct 29 '22 03:10

Gabriele Petrioli


document.getElementById('redbox').style.boxShadow = "0 0 3px #000";

like image 29
Lee Avatar answered Oct 29 '22 03:10

Lee