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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:44:57+00:00 2026-06-14T02:44:57+00:00

I am trying to use Javascript / JQuery to dynamically create several HTML buttons,

  • 0

I am trying to use Javascript / JQuery to dynamically create several HTML buttons, each of which does something different when it is clicked.

My first attempt looked like this:

function myFunc( target_div, num_buttons ) {
    var buttons="";
    for ( var i=0; i<num_buttons; i++ ) {
        buttons += '<input type="button" id="button_'+i+'" value="button_'+i+'"></input>';
    }
    target_div.html( buttons );
    var doButtonPress = function( i ) {
        alert( i ); // I actually do something more complicated here, but it's not important now
    }
    for ( var i=0; i<num_buttons; i++ ) {
        $('#button_'+i).click( function() { doButtonPress(i); } );
    }
}

Unfortunately this doesn’t work because (I think) when the click event happens, doButtonPress is always passed the current value of i in myFunc’s scope, not – as I intended – the value i had when the click event was defined. Thus the value passed to doButtonPress is always equal to num_buttons regardless of which button is pressed. So I then tried this instead:

$('#button_'+i).click( new Function( "doButtonPress("+i+");" ) );

Unfortunately this doesn’t work either – I get a message “ReferenceError: Can’t find variable: returnResult”. I assume that’s because the Function constructor’s arg value isn’t parsed until the click event happens, and at that time I’m already outside of myFunc’s scope so doButtonPress is undefined? Am I getting this right so far?

So … what do I do? 🙂

  • 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-14T02:44:58+00:00Added an answer on June 14, 2026 at 2:44 am

    Your code didn’t work because of the scope of variable i which would be num_button always inside the handler. You can fix it by wrapping it inside a closure but how about simplifying it by adding a class and making use of the id of the button.

    function myFunc( target_div, num_buttons ) {
        var buttons="";
        for ( var i=0; i<num_buttons; i++ ) {
            //                             added class ----------------v
            buttons += '<input type="button" id="button_'+i+'" class="mybuttons" value="button_'+i+'"></input>';
        }
        target_div.html( buttons );
        var doButtonPress = function( i ) {
            alert( i ); // I actually do something more complicated here, but it's not important now
        }
    
        //yes, mybuttons exist 
        $('.mybuttons').click(function () {
             doButtonPress(this.id.replace('button_', ''));
             //this.id.replace('button_', '') <- returns i value
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to use html and javascript to create a rotating logo on my
I am trying to use javascript/jquery. I have got below HTML where I want
I am trying to use javascript and jquery to build an HTML table based
I'm trying to use jQuery to identify <td> elements which contain specific HTML comment
I was trying to do something like Google's Adsense. I believe they use javascript?
I'm trying to use javascript and jquery to do some plotting, here is the
I am trying to use jQuery autocomplete on my dynamically created textboxes. this is
Hi i'm trying to use jQuery/javascript to make my life easier. I'm trying to
Hi jquery/javascript gurus, I am trying to use jquery ajax function to populate the
I am trying to use Javascript/jQuery to resize part of a website based on

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.