views:

59

answers:

2

How do I parse the border width from

style="border: solid 1px black;"

in jQuery/javascript?

$elem.css('border-width')

doesn't do it.

Note I need to parse the width from the css, as the element may be display:none

Thanks

Edit I'm not actually using an in-line style, I just wrote it that way for simplicity as I didn't realise there was any behavoural difference. It seems to work fine for inline styles though, but still can't get any value from an applied css class.

+3  A: 
GenericTypeTea
+1! Hint: Always include the radix: `parseInt(width, 10)`. Default is hexadecimal. `parseInt("08") === 0` and `parseInt("08", 10) === 8`
elusive
You can also use this derivative of ILMV's jsFiddle example to confirm that values set by shorthand properties still return properly: http://jsfiddle.net/s7YAN/1/
ssokolow
+1, I wasn't sure whether his question was having problems getting the property or parsing it.
ILMV
its not a parsing problem, i can't get any value for border width. See comment on the other answer.
fearofawhackplanet
@fearofawhackplanet - Answer updated.
GenericTypeTea
doh! i'd tried `border-left` but not `border-left-width`! what an idiot lol. thanks :)
fearofawhackplanet
No problemo. If you find a better way in the future, please update your question :). I can't believe this is the only way.
GenericTypeTea
+3  A: 

jQuery doesn't allow the use of - when referencing a CSS property, so remove the hypen and capitalise the following letter.

$elem.css('borderWidth');

Coded in jsFiddle to demonstrate.

ILMV
+1 because I originally wrote `border-width` in my answer.
GenericTypeTea
@ILMV: nope, still can't get it to work if the style isn't inline. see http://jsfiddle.net/s7YAN/2/
fearofawhackplanet