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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:42:24+00:00 2026-05-12T06:42:24+00:00

I am building a list with checkboxes, after the list is created I am

  • 0

I am building a list with checkboxes, after the list is created I am using “.each” to go through them all and trying to apply a click procedure.

My code like this works fine:

$.ajax({
    url: "./views/todoitems.view.php",
    data: "cmd=list",
    success: function(html){
        $('#list-container').empty().append(html);
        $('input:checkbox').each(function(){
            $check = $(this);
            $check.click(function(){
                alert($(this).attr('itemid'));
            });
        });
    }
});

However, as soon as I change the alert to be

alert($check.attr('itemid'));

It only ever shows the id of the last item in the list no matter which one is clicked.
What am I doing wrong?

  • 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-12T06:42:24+00:00Added an answer on May 12, 2026 at 6:42 am

    $check is set inside the each handler and is a global variable. That means it’ll retain the last value set, being the last checkbox. You need to set it inside the click handler if you want to do it right. Change it to:

    $.ajax({
      url: "./views/todoitems.view.php",
      data: "cmd=list",
      success: function(html) {
        $('#list-container').empty().append(html);
        $('input:checkbox').each(function(){
          $check.click(function() {
            var $check = $(this);
            alert($check.attr('itemid'));
          });
        });
      }
    });
    

    Note: var is used above to control scope.

    Also, you don’t need to use each() for this. This is equivalent:

    $.ajax({
      url: "./views/todoitems.view.php",
      data: "cmd=list",
      success: function(html) {
        $('#list-container').empty().append(html);
        $('input:checkbox').click(function() {
          alert($(this).attr('itemid'));
        });
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At the moment I'm building a list using the following code: $('<li id=li' +
I am building a checkbox lists: <asp:CheckBoxList ID=CheckBoxes DataTextField=Value DataValueField=Key runat=server></asp:CheckBoxList> And trying to
I'm building a Django todo list. The checkboxes to mark a task as complete
I'm building a list dynamically using: $(#Selected ul).append($liObject); Adding works, the issue is when
I am building a list of Users. each user has a FullName. I'm comparing
I am using the following code to build a list of lists: res =
I'm using jQMobile 1.0, and building a list of radio buttons dynamically from json
In my views.py, I'm building a list of two-tuples, where the second item in
I'm basically building a simple list, and one of the items in the list
I'm building a simple Todo List application where I want to be able to

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.