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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:09:34+00:00 2026-06-18T01:09:34+00:00

I am trying to write a generic table generator where I can tell it

  • 0

I am trying to write a generic table generator where I can tell it the function to call in the onclick event of each cell. The function is then defined somewhere else on the page.

The problem is I cannot get the page to evaluate the function at the right moment. I want it to convert my string to a function when it is building the DOM not when it fires the click event. Maybe this is not possible in Javascript/JQuery?

A simplified version of the code I originally wrote server-side in C# is:

string functionCall = "DoSomething(4);";
TableCell detailCell = new TableCell();
detailCell.Attributes.Add("onClick", functionCall);
functionCall = "DoSomething(5);";
detailCell = new TableCell();
detailCell.Attributes.Add("onClick", functionCall);

This then worked when it was rendered as html in the browser. It turned into:

 function DoSomething(p)
 {
      alert(p);
 }

<td onclick="DoSomething(4)"></td>
<td onclick="DoSomething(5)"></td>

I want to replicate this functionality in Javascript.

If I do:

var functionCall = 'DoSomething(4);';
$('#detailCell').attr('onclick', functionCall);

it won’t fire the event in IE7 for some reason but fires it in Chrome.

If I do:

var functionCall = 'DoSomething(4);';
$('#detailCell1').click(function () { eval(functionCall); });
functionCall = 'DoSomething(5);';
$('#detailCell2').click(function () { eval(functionCall); });

then it doesn’t change functionCall to a piece of text when the click event is added. Instead it evals functionCall WHEN the cell is clicked. This means the value of functionCall is now wrong.

e.g. it always does DoSomething(5) even if I click on detailCell1 because that was the last value of functionCall.

If I do:

var functionCall = 'DoSomething(4);';
$('#detailCell1').click(eval(functionCall));

it runs the function in functionCall immediately when adding the click event rather than when it is clicked.

I hope this makes some sense as it is difficult to explain.

Basically I just want to be able to do

<td onclick="any javascript I feel like storing in a string"></td>

using JQuery.

Is this even possible?

This is my first ever post on stackoverflow so please be gentle…

  • 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-18T01:09:35+00:00Added an answer on June 18, 2026 at 1:09 am

    Try this…

    var functionCall = 'DoSomething(4);';
    
    $('#detailCell1').click((function (code) {
      return function () { 
         eval(code); 
      };
    }(functionCall)));
    

    What happens here is that you capture the string at the time that you create the handler by assigning it to the parameter ‘code’.

    In your current scenario the click handler uses the value of the string at the time it is clicked and not at the time the handler is created.

    This is called a closure!

    (function (code) {
      return function () { 
         eval(code); 
      };
    }(functionCall));
    

    The functionCall value is assigned to the code parameter.
    Now when you change functionCall the code parameter still refers to the old value.

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

Sidebar

Related Questions

I'm trying to write a generic function that works with JSON arrays containing arbitrary
I am trying to write a function that will convert a generic list to
I'm trying to write generic code that can lex any stream of characters (
I'm trying to write a generic method that can be used to deserialize xml
I am trying to write a generic data transformation routine that is table driven
I'm trying to write some documentation and can't think of a generic name for
I'm trying to write a simple generic parallel code for minimizing a function in
I am trying to write a generic convert function but I don't know how
I'm trying to write a generic filtering function that performs linear interpolation at a
I am trying to write a generic function to be able to read a

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.