JQuery selector for an element without any attribute

Issue

I want to select an/all element which doesn’t have any attribute associated with it.is there any way to select particular element without considering what attributes other paragraphs tags contain?

For example:

<p class="a" id="para1">This is paragraph 1</p>
<p id="para2" class="a b c d e f" >This is paragraph 2</p>
......
......
<p>This is paragraph 3</p> <!-- select this p without attribute -->
<div>
  <p class="inside-div" id="para5">This is paragraph</p>
</div> <!-- select this div without attribute-->

Solution

To get all elements without attributes, use selector body>* along with the attributes filter (otherwise you get html/head/title etc)

$("body>*").filter(function() { 
    return this.attributes.length === 0; 
})

Simple fiddle here with OPs html

Answered By – freedomn-m

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published