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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:43:49+00:00 2026-06-11T09:43:49+00:00

I’m struggling with dynamic generation of buttons over a JSON array. Stripped-down code is

  • 0

I’m struggling with dynamic generation of buttons over a JSON array.

Stripped-down code is this (aim is to build a table based on the data, nothing fancy yet, I’m not yet proficient at this):

$.ajax({
    /* type, content, etc. removed */
    success: function (data, textStatus, XmlHttpRequest) {
        var target = $('myContainerDiv');
        var result = data.d.results;

        var $table = $('<table />');
        for(var i=0;i < results.length; i++) {
            var $row = $('<tr />');
            var $cell = $('<td />');
            var $button = $('<input />').attr({ type: 'button', value: 'Edit', name: 'btn' + i });
            $button.click(function () {

                // **
                // In a .NET environment, this would become a closure
                // I suspect this is the offending bit of code
                //

                alert(results[i].name);

            };

            $cell.append($button);
            $row.append($cell);
            $table.append($row);
        }

        $target.append($table);
    },
    /* error etc. removed*/
});

I basically want a column filled with buttons, each one would popup the value of a field from the array I get from my $.ajax call.

Buttons actually show up, but they do not react to clicking, and I see no runtime error in the F12 tools console. This is probably due to the fact that this script is part of the configuration page for a Microsoft Dynamics CRM 2011 Solution, but other than that, I’m sure the AJAX call goes on OK (I tried making it print out data, and I can see it).

UPDATE

Referencing i inside the click handler was the offending line indeed: changed the code like this made things work as I was expecting:

var $button = $('<input />').attr({ type: 'button', value: 'Edit', name: 'btn' + results[i].name });
$button.click(function () {
    // 'i' value is NOT what I thought it was !
    alert(this.name.substring(3,this.name.length));
    // I found out in the meanwhile that 'this' references the event source
};
  • 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-11T09:43:51+00:00Added an answer on June 11, 2026 at 9:43 am

    First you have several syntax errors in your code and it may not be running at all:

    1. if the ID of your div container is myContainerDiv, to get the target you need to do $('#myContainerDiv')

    2. you create a result varialbe, but you use a results variable

    3. you’re not closing the parentesis in the $button.click

    4. you’re adding everything to $target but is defined as target

    Now the actual problem may be, as you say, the closure, remember that you close over variables not values, so when you execute the button click handler, i has a value of results.length, so you are out of bounds by that time.

    You could try to store the results objects elsewhere, extract the Id of the object your looking for from the button (you’re naming then 'btn'+i) and then access the name property that way.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this

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.