Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JQuery to check if element has a border?

So I'm playing with $(el).css(), trying to determine if an element has a border. I use .css("border-style", "solid") to set the border, which works, but actually it sets 4 individual styles:

border-right-style
border-left-style
border-top-style
border-bottom-style

So, checking for a border is a bit cumbersome as you have to do something like:

if ($(el).css("border-right-style") == "solid" && $(el).css("border-left-style") == "solid" && ...) {}

Simply checking for $(el).css("border-style") != "" doesn't works since border-style is always equal to "".

Is there a more elegant way to do this?

like image 244
Tony R Avatar asked Jan 17 '12 18:01

Tony R


People also ask

How to check width in jQuery?

jQuery outerWidth() and outerHeight() MethodsThe outerWidth() method returns the width of an element (includes padding and border). The outerHeight() method returns the height of an element (includes padding and border).

Can a div have a border?

In modern responsive web development the div is a key page element. When using divs it can be useful to view its position on a page. Adding a border to the div achieves that. A border can also be used for decorative purposes.

What is border color in html?

Definition and UsageThe borderColor property sets or returns the color of an element's border. This property can take from one to four values: One value, like: p {border-color: red} - all four borders will be red.


3 Answers

border-style is Shorthand and you cant get them together so you have to get them separatly, because As per Jquery CSS documentation

Shorthand CSS properties (e.g. margin, background, border) are not supported.
For example, if you want to retrieve the rendered margin, 
use: $(elem).css('marginTop') and $(elem).css('marginRight'), and so on.
like image 57
dku.rajkumar Avatar answered Sep 22 '22 16:09

dku.rajkumar


If you want to know if 4 properties are ALL set, then yes you have to check 4 properties. Although I would cache the selector.

$el = $('a');
if ($el.css("border-right-style") == "solid" && $el.css("border-left-style") == "solid" && $el.css("border-top-style") == "solid" && $el.css("border-bottom-style") == "solid") 
{
    alert('yay');
}

jsFiddle

like image 31
Sinetheta Avatar answered Sep 24 '22 16:09

Sinetheta


I don't know if it is possible to do what you are trying. The DOM only provides the styles for the element that were assigned inline or to the element in script. It doesn't show if it inherited a style such as a border from CSS declarations.

like image 44
pseudosavant Avatar answered Sep 26 '22 16:09

pseudosavant