Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8967709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:18:22+00:00 2026-06-15T17:18:22+00:00

I have two elements that share a label. I need to add/remove a CSS

  • 0

I have two elements that share a label.

label shared by state and zip fields

I need to add/remove a CSS class on it as the two elements are being validated. If one or both are invalid – label needs to turn red. If both are valid – label is marked as valid.

Can’t figure out how to do it without entering an endless loop, where one element triggers the other’s validation, which in turn triggers the first one’s, etc. I can only think of using the highlight/unhighlight methods for now.

EDIT: Here is a jsfiddle to play with.

$("#my-profile-form").validate({
    errorContainer: "#errorProfile",
    highlight: function(el, errorClass, validClass) {
        var $el = $(el);
        if($el.attr('name') == 'state'){
            $el.addClass(errorClass).removeClass(validClass);
            $el.parents('fieldset').addClass('error');
        } else if ($el.attr('name') == 'zip') {
            $el.addClass(errorClass).removeClass(validClass);
        } else {
            $el.parents('fieldset').addClass(errorClass);
        }
    },
    unhighlight: function(el, errorClass, validClass) {
        var $el = $(el);

        if($el.attr('name') == 'state'){
            $el.removeClass(errorClass).addClass(validClass);
        } else if($el.attr('name') == 'zip') {
            $el.removeClass(errorClass).addClass(validClass);
        } else {
            $el.parents('fieldset').removeClass(errorClass);
        }
    },
    errorPlacement: function(error,element) {
        return true;
    },
    ignore: "", // needed to validate drop-downs in IE
    rules: {
        zip: {
            postalcode: true,
            required: true
        }
    },
    messages: {
        zip: {
            postalcode: " Please specify a valid zip code.",
            required: " The zip field is required."
        }
    }
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:18:23+00:00Added an answer on June 15, 2026 at 5:18 pm

    Ended up not using validator to solve the issue. I attach a ‘change’ handler to both elements and do all the work in there.

    var v_profile = $("#my-profile-form").data('validator');
    $("select.state, input.zip").on('change', { errorClass: v_profile.settings.errorClass, validClass: v_profile.settings.validClass }, function(ev){
        var validator = $(this).parents('form').data('validator');
        var res1 = validator.element( "select.state" );
        var res2 = validator.element( "input.zip" );
        if(res1 && res2){ 
            $(this).parents('fieldset').addClass(ev.data.validClass).removeClass(ev.data.errorClass); 
        } else { 
            $(this).parents('fieldset').addClass(ev.data.errorClass).removeClass(ev.data.validClass); 
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two HTML elements that share the same space- when one is visible,
Possible Duplicate: CSS Selector that applies to elements with two classes So I have
I have one form that has several elements and two submit buttons, one is
I have two identical click events for two different elements that do not share
We have two websites that share some business processes and UI elements. Lets focus
When you have two inline elements that share the same line, and the bigger
I have two span elements that I would like to stay on the same
I have two div elements that are twins (i.e. their dimensions and contents are
We have two lists: a=['1','2','3','4'] b=['2','3','4','5'] How to get a list with elements that
I have two functions that caluclate and adjust the height and width of elements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.