September 22, 2012

Jquery - Change/Replace All Css Class Names Inside a Parent

Use the below to change all the class names inside a parent


function replaceCSSClass()
{
    // Parent ID name
    var parentElement = "#divParentID"
 
    // Class Name, It is used to get classes that needs to be replaced.
    var selector= ".className"
 
    //Get All Elements
    var elements = jQuery(parentElement).find(selector);
 
 
    //Here 'oldClassName' is going to be replaced by 'className'
    jQuery.each(elements, function () {
        this.setAttribute
        (
            'class', 
            this.getAttribute('class').replace(/(?:^|\s)oldClassName(?!\S)/g, ' className')
        );
    });
}

No comments:

Post a Comment

Recommended Post Slide Out For Blogger