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 9165551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:56:11+00:00 2026-06-17T14:56:11+00:00

Why does this script add a class to every radio button and checkbox Label

  • 0

Why does this script add a class to every radio button and checkbox Label on the page, instead of just the ones for each individual checkbox/radio button?

For example, I have these buttons:

<input class="required" id="Example31" name="Example3" type="checkbox" />
<label for="Example31">Example Input 3 Option 1</label>

<input class="required" id="Example32" name="Example3" type="checkbox" />
<label for="Example32">Example Input 3 Option 2</label>

<input class="required" id="Example4" name="Example4" type="radio" />
<label for="Example4">Example Input 4</label>

And this script:

$("input.required").each(function() {
    if ($(this + ":checkbox") || $(this + ":radio")) {
        if (!$(this + ":checked").val()) {
            var inputId = $(this).attr("id");
            $("label[for=" + inputId + "]").addClass("error");
        };
    };
});

The script fires when I click a button. If no box is checked, every label gets an error added to it, which is correct. However, if one box is checked, no label gets an error on it, which is incorrect. If I check a checkbox in the first group, but leave the radio button unchecked, that radio button should be marked as an error.

NOTE: I know this script doesn’t account form the input names just yet (so if one checkbox with the same name is checked, the other would currently display an error), I’m trying to get his bug worked out before moving forward.

  • 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-17T14:56:12+00:00Added an answer on June 17, 2026 at 2:56 pm

    These lines of code are complete nonsense, they cannot do anything useful or sane:

    if ($(this + ":checkbox") || $(this + ":radio")) {
        if (!$(this + ":checked").val()) {
    

    I suspect you’re trying to do…

    if ($(this).is(":checkbox") || $(this).is(":radio")) {
        if (!$(this).is(":checked")) {
    

    By way of explanation,

    1. this + ":checkbox" will always produce a selector like [object Something]:checkbox, where Something will vary based on the value of this
    2. This will match :checkbox elements which have the attributes object and Something, (see Has Attribute selector) which is almost certainly not what you want and will very likely match no elements; Actually I’m not sure why this works; [object] is a valid selector, and [object Something]:checkbox is a working (but likely invalid – jQuery bug?) selector but [object Something] is an invalid selector and raises an error
    3. Regardless of whether elements were matched $("...) will always produce a jQuery object with 0 or more elements, which is always truthy. Instead of if ($(...)) {, you need to check if ($(...).length > 0)

    To summarize, you’re producing an invalid selector which (through some quirk of jQuery) doesn’t raise an error, but which returns completely bogus results.

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

Sidebar

Related Questions

What is the best way to add another class to this script: <script type=text/javascript>
Why does this script return a pair of nulls? I'm using SQL Server 2008,
I could use another pair of eyes. This script does what I want in
I have to write a Matlab script that does this: The input is 2
EDIT: It does work (sorry). Something in this script is causing it to stop
for the moment the script does like this: click on the link edit the
I am working on a Bash shell script that does something like this: #!/bin/bash
What does VAR_NAME=${VAR_NAME:-/some/path/file} mean in an shell script? This is for an init script,
I have a simple script that does some search and replace. This is basically
I have been working on this for some time. The script does work and

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.