0

I want ask if is possible check in JQuery, if a div have a certain property and if have a specific value. So take this example:

<div id="foo" style="width: 100%" />

I need to check if the div foo have the css property width and if this have a value of 100%.

What I can do for achieve this?

Ashish Bahl
  • 1,482
  • 1
  • 18
  • 27
AgainMe
  • 760
  • 4
  • 13
  • 33
  • http://stackoverflow.com/questions/24591327/jquery-check-if-element-has-a-specific-style-property-display – cloned Jan 18 '17 at 09:53
  • @Richard unfortunately that question gives the computed styles when it comes to width - the question would be more a duplicate of this one: http://stackoverflow.com/questions/16126670/return-css-height-with-jquery-not-computed-but-declared – Pete Jan 18 '17 at 09:59
  • Anyway - you need to use the native js object and use the style : `$('#foo').get(0).style.width` – Pete Jan 18 '17 at 10:01

1 Answers1

2

If you are trying to select the element based on inline style attribute then you can use attribute contains selector but this may fail when there is extra space within the defenition.

if (('#foo[style*="width: 100%"]').length)
  alert('true')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="foo" style="width: 100%" />

To resolve the issue with space you can use regex with RegExp#test method on style attribute.

if (/\bwidth\s?:\s?100%/.test($('#foo').attr('style')))
  alert('true')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="foo" style="width: 100%" />
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • uhm, nope, I'm trying to check if the div `foo` have the `width` property and also if this have a value of `100%`, actually I tried like so: `if($('#foo').css('width') == '100') { alert(true); }` but not seems to be working – AgainMe Jan 18 '17 at 09:57
  • 1
    @AgainMe : updated – Pranav C Balan Jan 18 '17 at 10:02
  • 1
    Ok so is enough a `length` for check this, never though to this. Thanks – AgainMe Jan 18 '17 at 10:03