70

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

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 behavioural difference. It seems to work fine for inline styles though, but still can't get any value from an applied css class.

1
  • 3
    In the past, I've avoided problems like this by using visibility: hidden or setting an absolute position far outside the screen (i.e. left: -10000px). If you could do that, a simple $(elem).outerWidth(false) - $(elem).innerWidth() would get you the border width. Commented Sep 24, 2010 at 13:26

5 Answers 5

100

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.

Sign up to request clarification or add additional context in comments.

14 Comments

+1! Hint: Always include the radix: parseInt(width, 10). Default is hexadecimal. parseInt("08") === 0 and parseInt("08", 10) === 8
@elusive The default is decimal. For example, parseInt(10) === 10. What you're demonstrating is that parseInt will interpret the input as octal if it has a leading "0".
Beware, though, that IE can return non-numbers for border-whatever-width, and the CSS spec allows values of "thin", "medium" and "thick" too. This jsfiddle shows IE returning "middle" as a border-top-width. FF and chrome show it as 0px, and parseInt("middle", 10) returns NaN, so be sure to test in IE too. I'm still looking for how to get a number of pixels in every case.
re IE, if you detect IE ... change everything to 50pt comic sans and add a popup message linking to a decent browser using setInterval
@SamWatkins I don't know how you've only 423 rep. That's the best advice I've ever seen on SO!
|
10
var bordersOnBothSides = $("#measureMe").outerWidth() - $("#measureMe").innerWidth() ); 

Comments

9

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.

5 Comments

nope, still can't get it to work if the style isn't inline. see jsfiddle.net/s7YAN/2
@Ben Everard - jQuery does allow hyphnes - its just short-hand CSS that is not allowed - so left-border-width is ok, but border-width is not.
Your statement is not true. Changed fiddle to border-width and it still works: jsfiddle.net/s7YAN/500
From jQuery's documentation: api.jquery.com/css --My experience is hyphens for CSS properties works within quotes. I use camelCase when setting multiple properties in an object, i.e.: $(this).css({borderWidth: "2px", borderColor: "blue"}); ..because hyphens are an operator when used outside quotes in JS, as opposed to a character.
This solution is not working at EDGE & FireFox, it will return string empty, you must calculate the border-left-width + border-right-width, etc.
7

To complete Generic's answer when seeking he left border and to include elusive's comment about radix:

<div style="border-style:solid;border-left-width:15px;"></div> 

in script:

var width =parseInt($('div').css('borderLeftWidth'),10); alert(width); 

Comments

3

If u have equal border widh for element, or u need left, top border width, plain js:

elem.clientLeft; //get left border of element elem.clientTop; //get top border of element 

To get right, bottom border use jquery+css:

parseInt($(elem).css('borderLeftWidth'),10); // 10 use to get result in dec, not hex number system 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.