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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:52:35+00:00 2026-05-31T12:52:35+00:00

I have the strangest thing going on here which I’m sure there’s a perfectly

  • 0

I have the strangest thing going on here which I’m sure there’s a perfectly rational answer to. I’m using jQuery which frankly I haven’t used to any substantial extent, so I’m guessing this is my own newbie-ism at work.

I am sending a settings-object to a function. This settings-object contains a struct by the name of “functions”. This “functions”-object is a struct where the jQuery identifiers are keys, and the values are the callback functions.

Then, in the target function (to which the settings-object gets sent), I iterate through the settings.functions object to bind each function to each respective target.

However, the $button.click() event always sets every control to the last of the settings.functions functions.

So, if I have 2 buttons, “Delete” and “Edit”, and I configure the settings object so that each will get their own, respective function… then for reasons that I’m trying to find, both buttons will receive the “Edit” function. Testing has shown that both buttons always get the last function in the settings.functions struct, no matter the key.

See “this.init = function…” code for the centre of weirdness. (snippet in middle)

The reason I’m trying to do it this way, is because in the call-back functions (configured in settings.functions), I need access to the $resource element from within $resources.each() clause. So in other words, from within the library function, I need to send the $resource object back to the callbacks, wherever they be defined.

Here’s the portion which calls the function:


$(document).ready(function() {

    var settings = {
        functions: {
            ".but-act-delete": function($row) {
                if (confirm("Are you sure you want to delete this resource? This action cannot be undone!")) {
                    /* Nevermind this
                    $page_action.val("resource-delete");
                    $resource_id.val($row.attr("resource_id"));
                    $page_form.submit();
                    */
                }
            },
            ".but-act-edit": function($row) {
                window.location = "admin-resource-edit.php?resource_id=" + $row.attr("resource_id");
            }
        }
    };

    var supertable = new SuperTable();
    supertable.init(settings);
});

Here’s the library function:


this.init = function(settings)
{
    var $resources = $("td.act");
    $resources.each(function(index, resource)
    {
        var $resource = $(resource);

        var button_class;
        for (button_class in settings.functions)
        {
            var $button = $resource.find(button_class);

            // This accurately shows ".but-act-edit" or ".but-act-delete"
            alert(button_class);

            $button.click(function()
            {
                // This ALWAYS shows ".but-act-edit"
                alert(button_class);

                // Find the proper callback.
                var func = settings.functions[button_class];

                // Call the call-back with the required element $resource.
                func($resource);
            });
        }
    });
}

Here is the HTML in question.


<td class="act" resource_id="<?php print $resource_id; ?>">
    <input type="button" class="but-act-edit" />
    <input type="button" class="but-act-delete" />
</td>

It appears that $button.click() assigns the event handler to all buttons every time, not just the one that it finds with $resource.find(button_class), which returns only one input each time (as it should).

Any help deeply appreciated. Especially if there’s a way to make what I’m doing unnecessary.

Thanks in advance.

  • 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-31T12:52:36+00:00Added an answer on May 31, 2026 at 12:52 pm

    This is the classic “using an outer loop variable in a callback” problem.

    Your button_class variable changes in the loop, but isn’t bound correctly in the callback, so its final value will always be used.

    The simplest fix in this case is to use:

    $.each(settings.functions, function(button_class, func) {
    })
    

    instead of:

    for (button_class in settings.functions)
    

    this will ensure that your button_class variable is correctly bound for each possible value.

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

Sidebar

Related Questions

I have a strange mysql-thing going on here, it is about the following code:
This is the strangest thing! I have a multi-threaded client application written in Python.
CSS newbie here. Strange thing is happening, I have gaps between links, and I
I have a table in my database which stores a tree structure. Here are
I have a very strange issue going on here. It's only occurring on Internet
I have a kind of strange thing that I really nead for my text
I found a very interesting/strange thing about MAX() function in SQL. I have column
I have the following query, now the strange thing is if I run this
during my debug sessions I found a strange thing occurring. I have an EditText
I have the strangest issue. I have a script that calculates what the current

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.