Micheasl
Micheasl

Reputation: 287

Jquery check if div has data attribute and remove

How can I check if a div has a data-attribute and remove the div if it has that data-attribute, the opposite works like this:

$("div[id='foo']").not('[data-type=edit]').remove();

Upvotes: 2

Views: 6540

Answers (3)

Rory McCrossan
Rory McCrossan

Reputation: 337743

Remove the not() and use the attribute in the main selector:

$('#foo[data-type=edit]').remove();

If you only want to find the element that has the data-type attribute, regardless of its value, you can use this:

$('#foo[data-type]').remove();

Upvotes: 5

MaanooAk
MaanooAk

Reputation: 2468

If you dont care about the value you can just do

$('#foo[data-type]').remove();

Upvotes: 0

Rana Ghosh
Rana Ghosh

Reputation: 4674

if(typeof $("#foo").attr('data-type') == 'undefined')
{
  $("#foo").removeAttr('data-type');
}

Upvotes: 1

Related Questions