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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:58:52+00:00 2026-05-28T00:58:52+00:00

I found similar questions while searching for a solution for my problem but I

  • 0

I found similar questions while searching for a solution for my problem but I don’t think its quite that what I’m looking for.

Using jQuery’s .on() is no brainer for binding events on dynamically created elements, but I got stuck while trying to pass different parameters to call different functions. After reading https://developer.mozilla.org/en/JavaScript/Guide/Closures I came up with the following working example, but I would like to know if its the best way to do it or if there is a more elegant solution?

http://jsfiddle.net/V25Zc/

Plus I often read that using eval() isn’t a best practice due to vulnerability issues but is there another way to build dynamic function names?

Edit:

Thanks a lot for everyone for the great answers! Did one of the answers got deleted though? I liked that one because of the hint to store the parameter in an attribute. Thanks again all!

  • 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-28T00:58:53+00:00Added an answer on May 28, 2026 at 12:58 am

    The point of event delegation is that you don’t need to bind an event to every single element (or to the same element multiple times), just once to the common parent, like so:

    function createLinks() {
    
        var $container = $("#container"),
            links = "";
    
    
    
        for (var i=0; i < 4; i++) {
            links += '<a class="linklink" href="#">Link_' + i + '</a>';
        }
    
        $container.html(links).on( "click", ".linklink", function(){
            console.log( "fn action"+$(this).index());
        });
    }
    
    createLinks();
    

    To avoid using eval, you could have an array of functions and call them by index:

    arrayOfFunctions[ $(this).index() ]();
    

    http://jsfiddle.net/V25Zc/2/

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

Sidebar

Related Questions

I found similar questions but no answer to what I am looking for. So
I've found similar questions but never the exact answer. I have Qt program that
I've been searching for the past 2 days, and while I've found similar questions
I've looked through similar questions but not found the exact working solution I'm after.
I found a few questions similar to this one here on SO, but none
I found a couple questions similar to this, but not specifically for .NET projects,
I've found a similar question here , but I'm looking for more general solutions.
While I found similar question I didn't find an answer to my problem When
I found similar questions asked here but there weren't answers to my satisfaction. So
i found few similar questions here but from answers i didn't get the whole

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.