estevan gomez estevan gomez - 2 years ago 95
jQuery Question

jQuery Find and Replace all instances of part of a classname

I have several div's with a classname that looks like this:


some have:


What I want to do is to search for whatever the number at the end of the class is and replace all of them to:


How can I do this using jQuery?

Answer Source

you can use "begin with" selector like this

$("[class^='col-md-']").removeClass (function (index, css) {
    return (css.match (/(^|\s)col-md-\S+/g) || []).join(' ');

removeClass function based on this answer


will find any element with a class that begins with col-md-. The removeClass function will than remove it. And finally col-md-6 will be added.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download