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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:49:53+00:00 2026-06-17T02:49:53+00:00

This works (in Firefox) … HTML <!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd>

  • 0

This works (in Firefox) …

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="src/myJS.js"></script>
</head>
<body></body>
</html>

Javascript file (called myJS.js for convenience)

window.onload = function()
{
    CreateInputTable();
};

CreateInputTable = function()
{
    var tbl = document.createElement('table');
    var tbo = document.createElement('tbody');
    var tr = document.createElement('tr');
    var td1 = document.createElement('td');
    var ib = document.createElement('input');
    ib.setAttribute('type', 'text');

    var tdID = "c1"; // Cell reference

    if (ib.addEventListener)// all browsers except IE before version 9 - see http://help.dottoro.com/ljeuqqoq.php
        {
            ib.addEventListener('change', foo, false);
        }
    else// IE before version 9 - see http://help.dottoro.com/ljeuqqoq.php
        {
            ib.attachEvent('change', foo, false);       
        };

    td1.appendChild(ib);
    tr.appendChild(td1);

    var td2 = document.createElement('td');
    td2.setAttribute('id', tdID);
    td2.appendChild(document.createTextNode("Hello world"));
    tr.appendChild(td2);

    tbo.appendChild(tr);
    tbl.appendChild(tbo);
    document.getElementsByTagName('body')[0].appendChild(tbl);
};

function foo (){
    if (document.getElementById("c1"))
        {
        document.getElementById("c1").appendChild(document.createTextNode(" and goodbye"));             
        }
};

However, I want to pass the cell reference “c1” to the event listener dynamically.

If I understand it correctly, I cant change the call to …

ib.addEventListener('change', foo(tdID), false);

because the parentheses will return the return value of foo, not foo as a function.

However, I can get it to work by changing the declaration of tdID to

this.var tdID = "c1";

… and foo to

function foo (){
    if (document.getElementById(tdID))
        {
        document.getElementById(tdID).appendChild(document.createTextNode(" and goodbye"));             
        }
}; 

If I understand it correctly, it works because foo is called within CreateInputTable, which means that it can see the this variables in CreateInputTable.

However, this won’t give me what I want because I want to create a second row with a new value for tdID. The above example seems to simply hard code the cell reference into foo.

How can I dynamically pass the cell reference to foo(in Object Oriented style)?

  • 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-17T02:49:54+00:00Added an answer on June 17, 2026 at 2:49 am

    An elegant way would be this:

    function foo (tdID){
        return function(){
            if (document.getElementById(tdID))
            {
                document.getElementById(tdID).appendChild(document.createTextNode(" and goodbye"));             
            }
        }
    }; 
    

    Then you can actually write

    ib.addEventListener('change', foo(tdID), false);
    

    because the call to foo(tdID9) will return a function with the right number of arguments and “knows” the correct value for `tdID’ because it is captured in the closure of the inner function.

    This technique is called currying.

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

Sidebar

Related Questions

I'm following the example here : http://www.codeproject.com/KB/aspnet/BilingualMvc3Part2.aspx Edit: This solution works for Firefox, and
<input type=button value=Back onClick=window.navigate('http://www.google.com')> This works on IE8, but not firefox or opera. Anyone
Example (works in Firefox): http://progamonth.com/files/tablestestfile.html I can't get select elements to dynamically get added
I have $('#structure').html(html); This works fine in FireFox. IE 8 doesn't display the html.
This works in Firefox and Chrome, but fails in IE. It does show results
I'm trying to figure out why this works in FireFox, Chrome but not in
This works perfectly in Firefox but doesnt work in ie i get the following
In Firefox's and Chrome's consoles, this works (alerts script content): var script = document.createElement(script);
Why doesn't this work <script src=jquery.js/> But this works <script src=jquery.js></script> ? Firefox 3.5.8
This is a problem where firefox works just fine, while chrome has the problem.

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.