Select all SPAN elements which contain a "foobar" property? If possible with jQuery

37 views
Skip to first unread message

Matt Adams

unread,
Mar 25, 2024, 4:53:35 AMMar 25
to greasemonkey-users

How can I select (from Tampermonkey script) all SPAN elements in current webpage which contain a property "foobar"?

The assigned value is unimportant.

So elements like the following should be matched:

<span aaa="bbb" foobar="23452345" .....>...</span>

I tried:

$('span[foobar^=""]').remove();

but it didn't work.

And

$('span').attr('foobar').remove();

didn't work either.

Jenna

unread,
Mar 25, 2024, 5:20:59 AMMar 25
to greasemonkey-users
When you use the prefix attribute selector [att^=val] then the value cannot be an empty string.

The jQuery method .attr(attributeName) returns the value of this attribute for the FIRST element. So in this case it would return the string "23452345". And strings don't have a .remove() method.

To select all spans that have the attribute foobar you can use the attribute presence selector [attr]. So in this example it would be span[foobar].

Brian L. Matthews

unread,
Mar 25, 2024, 12:56:31 PMMar 25
to greasemon...@googlegroups.com
You don't need jQuery for this, something like
document.querySelectorAll('span[foobar]') will return a live NodeList
with all the spans with attribute foobar (regardless of the attribute's
value) in it.

Brian
Reply all
Reply to author
Forward
Message has been deleted
0 new messages