Sean Popke Sean Popke - 1 year ago 122
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.


Any help would be appreciated.

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download