Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Remove DIVs based on class name

Tags:

html

jquery

Is it possible to remove all DIVs on a page via jQuery -- keeping their content intact -- which have this pattern to them?

<div class="ExternalClass85AC900AB26A481DBDC8ADAE7A02F039">....</div>

Note that the DIVs follow this pattern:

  • class name will always start with ExternalClass but will be followed by another value, never the same,

  • and the DIV has no other attributes but class. (This is on SharePoint)

Thanks.

like image 634
Alex Avatar asked Dec 16 '22 04:12

Alex


2 Answers

Yes, you can use the attribute starts with selector and the replaceWith method:

$('[class^="ExternalClass"]').filter(function() { 
  return this.attributes.length === 1; 
}). replaceWith(function() {
  return $(this).html();
});
like image 88
Rich O'Kelly Avatar answered Dec 18 '22 18:12

Rich O'Kelly


$('[class^="ExternalClass"]').replaceWith(function() {
    return $(this).html();
});
like image 23
dfsq Avatar answered Dec 18 '22 16:12

dfsq