Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get border width in jQuery/javascript

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.

like image 396
fearofawhackplanet Avatar asked Sep 24 '10 13:09

fearofawhackplanet


People also ask

How do I reduce the size of a border in HTML?

Adjusting the Margin Size of an HTML Element With CSS You can remove this margin by setting the top and left margin to zero. Like the padding and border, the sizes of specific sides of the margin can be set using margin-left , margin-right , margin-top , and margin-bottom .


2 Answers

You can just use parseInt(); to parse the border width from Npx to N:

var width = $elem.css('borderWidth'); // 150px var parsed = parseInt(width);         // 150 

I had a Google around and it appears in order to get the width of a border you must provide an explicit side of the border:

var borderWidth = $elem.css("border-left-width"); 

This is because it's possible for every border to have a different size, style and colour. Unfortunately it doesn't appear to be any simpler than this.

like image 178
djdd87 Avatar answered Oct 09 '22 09:10

djdd87


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.

like image 38
Ben Everard Avatar answered Oct 09 '22 11:10

Ben Everard