Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find an element that does not have an id, name, class, attribute, etc.?

I want to find (probably using jquery) an element that is empty/naked/etc. For example I want to find all the span elements that do not have an id, name, class, or any other attribute whatsoever.

<html>
<head></head>
<body>
    <span> found </span>

    <span id="foo"> not found </span>

    <span name="foo"> not found </span>

    <span class="foo"> not found </span>

    <span style="width:foo"> not found </span>

    <span> found </span>

    <span foo="bar"> not found </span>
</body>
</html>
like image 745
irrational Avatar asked Dec 15 '22 00:12

irrational


1 Answers

Thanks @adeneo for the information, this.attributes always returns a value. As such, I'll clean up the answer.

How to select elements that are "naked" (that do not have any attributes) using jQuery:

$('span').filter(function(){
    return (this.attributes.length === 0);
}).text();

The .text() method is just a placeholder, anything can be applied at that point.

What the hell, here's a simple jsFiddle.

like image 50
PlantTheIdea Avatar answered Apr 09 '23 19:04

PlantTheIdea