Showing posts with label JQuery. Show all posts
Showing posts with label JQuery. Show all posts

September 22, 2012

Jquery - Validate Multiple input controls Inside a Parent

function fnValidateChildFields(parentElement, selector) {
    var isValid = true;
 
    //Get Required Elements inside "Parent Element" By using selector
    //It will return filtered elements.
    var elements = jQuery(parentElement).find(selector);
 
    //For Each all fields and do your stuff
    jQuery.each(elements, function () {
 
        // Do validation Here..
 
        /* SAMPLE VALIDATION STARTS HERE */
 
        alert(this.id);
        alert(this.value);
        alert(this.getAttribute('class'));
 
        // Required Field validation
        if (this.value.length == 0)
            isValid = false;
 
        /* SAMPLE VALIDATION ENDS HERE */
 
    });
 
    return isValid;
}
 
 
function fnValidatePageII() {
    var isValid = false;
    try {
 
        // Parent ID name
        var parentElement = "#divParentID"
 
        // Class Name, It is used to get classes that needs to be replaced.
        var selector = ".className"
 
        if (fnValidateChildFields(parentElement, selector) == true)
            isValid = true;
    }
    catch (e) {
        alert(e);
    }
    return isValid;
}

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')
        );
    });
}