Select elements where attribute is non-empty
This works, if you don't mind doing things slightly backwards and you need it to work in browsers that don't support :not
:
div[data-pid] {
color: green;
}
div[data-pid=""] {
color: inherit;
}
That will make all the div
s with non-empty data-pid
s green.
Fiddle here: http://jsfiddle.net/ZuSRM/
jQuery Find elements with non empty data attribute
Use .filter()
This might get you started
$('button').click(function(){
$('div.item').filter(function(i){
return $(this).attr('data-value') != "";
}).css('background','yellow');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="container">
<div class="item" data-value="1">One</div>
<div class="item" data-value="2">Two</div>
<div class="item" data-value="">Three</div>
<div class="item" data-value="3">Four</div>
<div class="item" data-value="">Five</div>
<div class="item" data-value="4">Six</div>
<div class="item" data-value="">Seven</div>
</div>
<button>Find MT Data</button>
Select elements with empty or not-specified attribute
Basically, don't.
It's not good practice to put all your logic into the selector. It will end up being computationally highly expensive (because the parts need to be parsed out of a string before they are interpreted) and messy. Use the beauty of the filter
method instead:
$('span')
.filter(function(){
return !$(this).attr('cust-attr');
});
This removes all elements where cust-attr
is a non-empty string from the selection.
CSS attribute selector for non-empty attributes
try this
<style>
[Data-Attribute]:not([Data-Attribute=""])
{
background-color: Red;
}
</style>
jQuery: Select data attributes that aren't empty?
try
$(':not([data-go-to=""])')
UPDATE:
For the sake of not leading anyone astray, this answer will work in older versions of jQuery but is not future-proof. Since @gmo and @siva's answers both seem to be working with later versions I defer to (and encourage you to upvote) their answers....and of course hope you have a fantastic day.
If Attribute value is not empty in given xml, get the attribute value
Select item with @Type="CARMENT" and then it's not-empty ID
//*[@Type="CARMENT"]/@ID[not(.="")]
How to get the number of elements with a non-empty class name
You could create an array from the collection, and filter
by whether its className
is digital:
const psWithClass = document.querySelectorAll("p[class]");
const countPsWithNumericClass = [...psWithClass]
.filter(({ className }) => /^\d+$/.test(className))
.length;
console.log(countPsWithNumericClass );
<p class="">Hello, world!</p>
<p class="5">Hello, world!</p>
<p class="9">Hello, world!</p>
<p class="">Hello, world!</p>
Related Topics
Rstudio 0.98.1028 Add Background Image Only to Title Slide
Chrome 10/Windows @Font-Face Encoding Trouble
How to Implement Read More/Read Less in Pure CSS
How to Draw an Incomplete Circle with CSS and in It How to Put a Picture
Webpack Can Not Load Font File: Unexpected Token
Extend Child Div Beyond Container Div
Applying a Single Background Gradient to a Table Row
Select Elements Where Attribute Is Non-Empty
Why Do Inline-Blocks Break After Non-Breaking Space
Adding Border-Radius for Embedded Youtube Video
How to Change the Button Color When It Is Active Using Bootstrap
How to Hide Elements Not Specified in Grid-Template-Areas
What Does an Asterisk Before an Equal Sign Mean (*=) ? What About the Exclamation Mark
Flexbox, Min-Height, Margin Auto and Internet Explorer
@Font-Face Declarations Don't Work in Android 4.3 Internet Browser
Why Does Grid-Gap Cause an Overflow
How to Change Svg Fill Color When Used as Base-64 Background Image Data