0

I want to pass -webkit-background-clip property with javascript.

Because of the hyphen at start, I cant seem to get it right. Tried it with [, ", ' etc. How do I escape dash?

document.getElementById("menu").style.-webkit-background-clip = "text";
WhatisSober
  • 988
  • 2
  • 12
  • 32

2 Answers2

3

You can use bracket notation...

document.getElementById("menu").style["-webkit-background-clip"]

...or use camel case...

document.getElementById("menu").style.webkitBackgroundClip 

jsFiddle.

Note that JavaScript resolves these to different names, but the browser supports both.

alex
  • 479,566
  • 201
  • 878
  • 984
  • Tried that, didnot work! Chrome's console says: Uncaught ReferenceError: Invalid left-hand side in assignment! – WhatisSober Mar 23 '13 at 22:37
  • 5
    @NabRaj That doesn't sound related to the code you posted. Sounds more like you attempted to do assignment to a constant. – alex Mar 23 '13 at 22:41
  • yea, every line with hyphen(background-color etc) was throwing error. Using DOM(camel case) worked. Thanks again! – WhatisSober Mar 23 '13 at 22:45
  • @Nab: I believe you miss quotation marks in the hyphen solution and the engine things you are trying to do a subtraction. The error you mentioned would not be generated by `document.getElementById("menu").style["-webkit-background-clip"]`. – Felix Kling Mar 23 '13 at 22:48
1

try document.getElementById("menu").style["-webkit-background-clip"] = "text";

OpherV
  • 6,787
  • 6
  • 36
  • 55