Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Style property names -- going from the regular version to the JS property camelCase version and vice versa

Tags:

javascript

css

Does there exist a provision for obtaining the corresponding names?

A function I'm writing has to both set the style via element.style[propnameCamelCase] and retrieve the existing rendered value via document.defaultView.getComputedStyle(element,'').getPropertyValue(propname-regular), and I can hardly justify having to pass two separate but semantically identical arguments to this function.

I know that for most of them it's a fairly straightforward transcription between camelCase and hyphen-delimited with the same words, so I can use regexes to convert them. But maybe there are a few that are not like this?

Off the top of my head I'm having a hard time figuring out how to deal with the capitalized letters for camel case with regular expressions.

edit: Ah, I could use a function for regex replace, each time I see a hyphen, convert next letter to upper case.

like image 693
Steven Lu Avatar asked Oct 10 '11 16:10

Steven Lu


1 Answers

So you're basically reinventing jQuery.css(). Maybe a look at how jQuery solved the camelCase problem might help: https://github.com/jquery/jquery/blob/master/src/core.js#L600

like image 188
rodneyrehm Avatar answered Nov 14 '22 20:11

rodneyrehm