I'm trying to select all elements that have a data-go-to
attribute that is not empty.
I've tried $('[data-go-to!=""]')
but oddly enough it seems to be selecting every single element on the page if I do that.
I'm trying to select all elements that have a data-go-to
attribute that is not empty.
I've tried $('[data-go-to!=""]')
but oddly enough it seems to be selecting every single element on the page if I do that.
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.
$('element:not([attribute=])'); // gets all of <element attribute="">
or $(':not([attribute=])'); // gets all of <* attribute="">
–
Mendelian $('[data-go-to!=""]:[data-go-to]')
does. –
Tried $("[data-go-to!=''][data-go-to]")
) –
Refluent Just as further reference, and an up-to-date (may'14) (aug'15) (sep'16) (apr'17) (mar'18) (mar'19) (may'20) (jan'22)...
Answer that works with:
###Empty strings: If the
attr
must exist & could have any value (or none at all)
jQuery("[href]");
###Missing attributes: If
attr
could exist & if exist, must have some value
jQuery("[href!='']");
###Or both: If
attr
must exist & has to have some value...
jQuery("[href!=''][href]");
PS: more combinations are possible...
###Check this test in jsFiddle for examples:
jQuery v1.9.1 ->
jsFiddle online testjQuery v1.11.0 ->
jsFiddle online testjQuery v2.1.0 ->
jsFiddle online testjQuery v2.1.3 ->
jsFiddle online testjQuery v2.2.4 ->
jsFiddle online testjQuery v3.2.1 ->
jsFiddle online testjQuery v3.3.1 ->
jsFiddle online testjQuery v3.4.1 ->
jsFiddle online test Last jQuery version available in jsFiddle at jan 10'22jQuery Edge ->
jsFiddle online test jQuery edge version (use with caution)jQuery v3.0.0-alpha1 ->
jsFiddle online testjQuery v3.1.1 Slim ->
jsFiddle online testjQuery v3.2.1 Slim ->
jsFiddle online testjQuery v3.3.1 Slim ->
jsFiddle online testjQuery v3.4.1 Slim ->
jsFiddle online test Last jQuery Slim version available in jsFiddle at jan 10'22###Or here in SO with this Code Snippet.
* Snippet is running jQuery v2.1.1
jQuery('div.test_1 > a[href]').addClass('match');
jQuery('div.test_2 > a[href!=""]').addClass('match');
jQuery('div.test_3 > a[href!=""][href]').addClass('match');
div,a {
display: block;
color: #333;
margin: 5px;
padding: 5px;
border: 1px solid #333;
}
h4 {
margin: 0;
}
a {
width: 200px;
background: #ccc;
border-radius: 2px;
text-decoration: none;
}
a.match {
background-color: #16BB2A;
position: relative;
}
a.match:after {
content: 'Match!';
position: absolute;
left: 105%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test_1">
<h4>Test 1: jQuery('a[href]')</h4>
<a href="test">href: test</a>
<a href="#">href: #</a>
<a href="">href: empty</a>
<a>href: not present</a>
</div>
<div class="test_2">
<h4>Test 2: jQuery('a[href!=""]')</h4>
<a href="test">href: test</a>
<a href="#">href: #</a>
<a href="">href: empty</a>
<a>href: not present</a>
</div>
<div class="test_3">
<h4>Test 3: jQuery('a[href!=""][href]')</h4>
<a href="test">href: test</a>
<a href="#">href: #</a>
<a href="">href: empty</a>
<a>href: not present</a>
</div>
$(':not([data-go-to=""])')
is not working anymore –
Refluent 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.
$('element:not([attribute=])'); // gets all of <element attribute="">
or $(':not([attribute=])'); // gets all of <* attribute="">
–
Mendelian $('[data-go-to!=""]:[data-go-to]')
does. –
Tried $("[data-go-to!=''][data-go-to]")
) –
Refluent $('[data-go-to!=""]:[data-go-to]').each(function() {
// Do Your Stuff
});
Unrecognized Expression
error. –
Considering Has 'data-attributename' and its value is not empty:
$('[data-attributename]:not([data-attributename=""])')
Has 'data-attributename' empty or not:
$('[data-attributename]')
I'm not sure about a simple selector, but you could use filter()
:
$('[data-go-to]').filter(
function(){
return ($(this).attr('data-go-to').length > 0);
});
References:
$('[data-go-to]').filter(function() {
return $(this).data('go-to')!="";
});
Using :not
, .not()
, :empty
etc will only check if the element itself is empty, not the data attribute. For that you will have to filter based on the data attributes value.
This works for me
$('[attributename]').filter('[attributename!=""]')
Try this :
$('[data-go-to:not(:empty)]')
"Syntax error, unrecognized expression: [data-go-to:not(:empty)]"
–
Enunciate © 2022 - 2024 — McMap. All rights reserved.