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

  • Home
  • SEARCH
  • 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 6571215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:51:13+00:00 2026-05-25T14:51:13+00:00

I am using the JQuery Validation plug-in and my goal is for the labels

  • 0

I am using the JQuery Validation plug-in and my goal is for the labels of invalid fields to have the error class applied to it. I managed to do this, but my problem is that JQuery is overwriting the label’s existing class rather than appending the error class to it.

Here’s the JQuery:

<script src="<?php bloginfo('template_directory'); ?>/js/lib/jquery.js" type="text/javascript"></script>
<script src="<?php bloginfo('template_directory'); ?>/js/jquery.validate.js" type="text/javascript"></script>
<script>
    $(document).ready(function(){
        $("#profile-basic").validate({
            debug:true,
            highlight: function(element, errorClass, validClass) {
                $(element).addClass(errorClass).removeClass(validClass);
                $(element.form).find("label[for=" + element.id + "]")
                .addClass(errorClass);
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).removeClass(errorClass).addClass(validClass);
                $(element.form).find("label[for=" + element.id + "]")
                .removeClass(errorClass);
            }
        });
    });
</script>

The highlight and unhighlight options came off the JQuery web site. (http://docs.jquery.com/Plugins/Validation/validate)

Here’s the HTML – the elements in question are the label and select for categoryID:

<form method="post" action="" class="bullhorn" id="profile-basic">
    <div id="accordion">  <!-- Accordion is currently disabled -->
        <h3>Name*</h3>
        <div>
            <div class="twocol">
                <label for="firstName">First Name:*</label>
                <input type="text" id="firstName" name="firstName" class="required" />

                <label for="middleName">Middle Name:</label>
                <input type="text" id="middleName" name="middleName" />

                <label for="lastName">Last Name:*</label>
                <input type="text" id="lastName" name="lastName" class="required" />
            </div>
            <div class="twocol">
                <label for="categoryID" class="ownline">Please select your primary category:*</label>
                <select name="categoryID" id="categoryID" class="required">
                    <option value="">Category 1</option>
                    <option value="">Category 2</option>
                    <option value="">Category 3</option>
                    <option value="">Category 4</option>
                    <option value="">Category 5</option>
                    <option value="">Category 6</option>
                    <option value="">Category 7</option>
                    <option value="">Category 8</option>
                    <option value="">and so on</option>
                </select>
            </div> 
        </div><!-- name -->
    </div>
    <div class="buttons"><input type="submit" value="Save and Go to Work History"/><input type="submit" value="Save"/></div>
</form>

Note that accordion is currently disabled – commented out completely.

And the relevant CSS:

label { float:left; text-align:right; width:30%; margin:0 14px 14px 0; line-height:20px; font-weight:bold; }
input, select, textarea { float:left; width:63%; margin-bottom:14px; background:#eee; line-height:1; }
    input[type=submit],  input[type=reset] { float:right; display:block; width:auto; margin:20px 0 0 14px; }
input:focus, textarea:focus { background:#fff; }
input, textarea { padding:2px 4px; }
select option { text-transform:capitalize; }
textarea { height:49px; }

.ownline { text-align:left; width:100%; line-height:20px; font-weight:bold; margin-bottom:3px; }
    .ownline+input,  .ownline+select,  .ownline+textarea, .ownline+.fieldinstructions+select { width:100%; }

label.error { color:#8e082d; }
    label.error:before { content: "\00bb\00a0"; display:inline-block; text-indent:-10px; }
input.error { border:2px solid #8e082d; background-color:#fdf5f3; } 

When I submit the form (with no filled out fields so that errors are generated) The class ownline on the categoryID label is removed and replaced by the error class. At least it seems to be the case. When I view the pages’ source, the categoryID label looks like: below. (I removed the select options for brevity.)

<label for="categoryID" class="ownline">Please select your primary category:*</label>
<select name="categoryID" id="categoryID" class="required">
</select>

But in Firebug it looks like:

<label class="error" for="categoryID">Please select your primary category:*</label>
<select id="categoryID" class="required error" name="categoryID">
</select>

Interestingly, JQuery overwrote the existing class in the label, but appended to the existing class in the select.

In case this was a CSS specificity problem, I did test writing the ownline styles with label.ownline as the selector, but it didn’t solve it.

This is a WordPress site with Contact Form 7 installed, but I added a function to functions.php so Contact Form wouldn’t call its stuff on the page in question.

I’ve been Googling this for hours.

I’m definitely a JQuery noob. I’d love some help, but please speak slowly. 🙂

Thank you thank you,
Kim

  • 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-05-25T14:51:14+00:00Added an answer on May 25, 2026 at 2:51 pm
    $(document).ready(function(){
        $("#profile-basic").validate({
            debug:true,
            errorElement: "nothing", // <-- this works !! (or "div", or something else)
            highlight: function(element, errorClass, validClass) {
                $(element).addClass(errorClass).removeClass(validClass);
                $(element.form).find("label[for=" + element.id + "]")
                .addClass(errorClass);
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).removeClass(errorClass).addClass(validClass);
                $(element.form).find("label[for=" + element.id + "]")
                .removeClass(errorClass);
            }
        });
    });
    

    and I’m sure 😉

    point is, the validate plugin has is own error management method, and by default it uses the nearest “label” it finds, here we tell this plugin module to look somewhere else to do it’s stuff.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm using this plug in to validate a form http://bassistance.de/jquery-plugins/jquery-plugin-validation/ I've got 3 fields,
Using jQuery validation plugin but it has no CSV validation. I have made an
I am using jQuery validation plug-in 1.7 for a page with form fields. The
I'm using jQuery along with the jQuery validation plug-in. I have a table where
I'm using the jQuery Validation plug-in. I have overwritten the required rule since mine
I'm using this two plug-ins jquery.validate and jquery.form separately, but I would like to
Using jQuery validation, if I have many fields on a page, and only require
I am using jquery validation plugin for form validation. I have added a custom
I'm trying to be able to place the error message when using jQuery validation
I'm using the jQuery Validation plugin and I've got a textbox with the class

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.