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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:01:37+00:00 2026-05-17T00:01:37+00:00

Using JQuery, is there a more efficient way of writing this code? See below

  • 0

Using JQuery, is there a more efficient way of writing this code? See below

The JQuery:

$("#ex1").live("click", function(){
if($("#ex1").attr("checked")==true){ 
    $(this).parent().removeClass("im-unchecked").addClass("im-checked"); 
}
else if($("#ex1").attr("checked")==false){ 
    $(this).parent().removeClass("im-checked").addClass("im-unchecked");
}});


$("#ex2").live("click", function(){
if($("#ex2").attr("checked")==true){ 
    $(this).parent().removeClass("im-unchecked").addClass("im-checked"); 
}
else if($("#ex2").attr("checked")==false){ 
    $(this).parent().removeClass("im-checked").addClass("im-unchecked"); 
}});

The HTML I’m working with:

<label for="ex1" id="ex-label-1">Y <input type="checkbox" id="ex1" name="ex" value="Y" <% If IsEx("Y") then%>checked<%end if%> /></label>
<label for="ex2" id="ex-label-2">M <input type="checkbox" id="ex2" name="ex" value="M" <% If IsEx("M") then%>checked<%end if%> /></label>
<label for="ex5" id="ex-label-3">X <input type="checkbox" id="ex5" name="ex" value="X" <% If IsEx("X") then %>checked<%end if%> /></label>
<label for="ex3" id="ex-label-4">N <input type="checkbox" id="ex3" name="ex" value="N" <% If IsEx("N") then %>checked<%end if%> /></label>
<label for="ex6" id="ex-label-5">L <input type="checkbox" id="ex6" name="ex" value="L" <% If IsEx("L") then %>checked<%end if%> /></label>
<label for="ex7" id="ex-label-6">Z <input type="checkbox" id="ex7" name="ex" value="Z" <% If IsEx("Z") then %>checked<%end if%> /></label>
<label for="ex4" id="ex-label-7">A <input type="checkbox" id="ex4" name="ex" value="A" <% If IsEx("A") then %>checked<%end if%> /></label>    

Sorry guys I’m a novice as JavaScript/JQuery

Any help would be greatly appreciated, Thanks

  • 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-17T00:01:37+00:00Added an answer on May 17, 2026 at 12:01 am

    You can use a name attribute selector and cut down on the code using .toggleClass() like this:

    $("input[name='ex']:checkbox").live("click", function(){
      $(this).parent().toggleClass("im-unchecked", !this.checked)
                      .toggleClass("im-checked", this.checked); 
    });
    

    You can give it a try here.

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

Sidebar

Related Questions

I am wondering which way is more efficient using jQuery data to bind data
Is there a more efficient way of declaring and using these (very similar/repetitive) CSS
using jQuery I have the following code: var selectedIdsArray = $(#selectId option:selected).map(function(){return this.value;}); var
Using JQuery, is there a simple way to select the text immediately after a
Using jquery UI 1.8's autocomplete, is there any known way to take the results
Is there a way using JQuery that i can detect when a div starts
Is there a way to get HTML contents from TinyMCE editor using jQuery so
When using jQuery events such as click, mouseover and so, are there big differences
Is there a way to delay css from firing using jquery? I have some
Is there a way, using Jquery to grow and then shrink an image (with

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.