JQuery - remove div based on class name

Is it possible to delete all DIVs on a page through jQuery - keep their contents intact - that have this template for them?

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

Note that DIVs follow this pattern:

  • the class name always starts with ExternalClass , but it will be followed by a different value, never the same,

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

Thanks.

+4
source share
8 answers

Yes, you can use attribute with selector and replaceWith :

 $('[class^="ExternalClass"]').filter(function() { return this.attributes.length === 1; }). replaceWith(function() { return $(this).html(); }); 
+4
source
 $('[class^="ExternalClass"]').replaceWith(function() { return $(this).html(); }); 
+2
source

Get internal html from div. Get the parent or previous tag to insert content in or after deleting the div. JQuery may have a replacement function, but not sure if it would be easier.

Must be able to use .remove () to get rid of the div.

.

$ ("[class $ = 'ExternalClass']") remove ();
maybe id * = i forget which one.

Corrected option: http://api.jquery.com/replaceWith/

+1
source

You need to rewrite the HTML code.

$("div [class^=ExternalClass]") will select the elements.

Take innerHTML, add it after the element, then delete the element.

+1
source

You can try this using the attribute starts with the ^= selector.

 $('div[class^="ExternalClass"]').each(function(){ $(this).replaceWith($(this).html()); }); 
+1
source
 var $text = ""; $.each($("div"), function(){ $text = $text + this.html(); }); document.write($text); 
+1
source

HTML sample:

 <div class="ExternalClass85AC900AB26A481DBDC8ADAE7A02F039">foo</div> <div class="ExternalClass85AC900AB26A481DBDC8ADAE7A02sdfsdsdfF039">bar</div> 

jQuery sample:

 $('div[class^="ExternalClass"]').each(function() { $(this).contents().unwrap(); }); 

Output:

 foo bar 

(wihtout divs)

sees the violin:

http://jsfiddle.net/6hbhL/

+1
source

My code is:

 function unrichText(texto) { var n = texto.indexOf("\">"); //Finding end of "<div&nbsp;class="ExternalClass..."> var sub = texto.substring(0, n+2); //Adding first char and last two (">) var tmp = texto.replace(sub, ""); //Removing it tmp = replaceAll(tmp, "</div>", ""); //Removing last "div" tmp = replaceAll(tmp, "<p>", ""); //Removing other stuff tmp = replaceAll(tmp, "</p>", ""); tmp = replaceAll(tmp, "&#160;", ""); return (tmp); } function replaceAll(str, find, replace) { return str.replace(new RegExp(find, 'g'), replace); } 
0
source

Source: https://habr.com/ru/post/1395047/


All Articles