// add class row to sp-column when children has any part of class with span in the classname jQuery(document).ready(function ($) { $('.sp-column').each(function () { // get the children class names with span in the classname var childrenClasses = $(this).children().map(function () { return this.className; }).get().join(' '); // if children has any part of class with span in the classname if (childrenClasses.indexOf('span') > -1) { $(this).addClass('row'); } // change parte of classname with span to col-md- $(this).children().each(function () { var className = $(this).attr('class'); className = className.replace(/span/g, 'col-md-'); $(this).attr('class', className); }); }); });