Sean Popke Sean Popke - 5 months ago 21
jQuery Question

Replace H3 tag with H2 if content is uppercase using jquery

I have html content extracted from a PDF file. I need to convert all H3 tags (with only uppercase content) to H2 tags. H3 tags with uppercase/lowercase content will be left alone.

I'm using this to convert H3 tags to H2, but not sure how to only have it apply to tags with uppercase content.

$('h3').contents().unwrap().wrap('<h2/>');


Any help would be appreciated.

Answer

You can use the .filter method:

$('h3').filter(function() {
   // check if all letters are in uppercase
   return this.textContent === this.textContent.toUpperCase();
}).replaceWith(function() {
   return $("<h2></h2>").append(this.childNodes);
});
Comments