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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:23:10+00:00 2026-06-04T09:23:10+00:00

I just developed a little code to create a 24×60 table. I want to

  • 0

I just developed a little code to create a 24×60 table. I want to print the id of each <td> on mouseover:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
table {
    background-color:blue;
}
td {
    width: 2px;
    height: 2px;
    background-color:red;
}
</style>
</head>
<body>
<table id="time-table"></table>
<script type="text/javascript">
var table = document.getElementById( "time-table" );
for ( var r = 0; r < 24; r++ ) {
    var row = document.createElement( "tr" );
    for ( var c = 0; c < 60; c++ ) {
        var td = document.createElement( "td" );
        td.id = "td-" + r + "-" + c;
        td.onmouseover = function ( e ) {
            console.log( this.id );
        }
        row.appendChild( td );
    }
    table.appendChild( row );
}
</script>
</body>
</html>

The code works, but now I’m concerned if it is optimized? Am I creating 1440 event handling functions in the nested loops? Or is the JavaScript interpreter smart enough to only create one function and assign it to 1440 <td> elements?

  • 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-04T09:23:12+00:00Added an answer on June 4, 2026 at 9:23 am

    No, JavaScript won’t to any optimizations (well, maybe some implementations do, but you should not rely on that). You are really creating that many functions.

    Better define the function once and reuse it:

    var handler = function() {
        console.log(this.id);
    }
    
    for ( var r = 0; r < 24; r++ ) {
        var row = document.createElement( "tr" );
        for ( var c = 0; c < 60; c++ ) {
            var td = document.createElement( "td" );
            td.id = "td-" + r + "-" + c;
            td.onmouseover = handler;
            row.appendChild( td );
        }
        table.appendChild( row );
    }
    

    Or consider to use event delegation, that is, binding the handler to an ancestor of the cells:

    table.onmouseover = function(event) {
        event = event || window.event;
        var target = event.target || event.srcElement;
    
        if(target.nodeName === 'TD') {
            console.log(target.id);
        }
    };
    

    This works, since events bubble up the DOM tree and might even better performance-wise in some browsers.

    A good resource to learn about event handling are the articles at quirksmode.org.

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

Sidebar

Related Questions

I want to develop a little iPhone application, just for my personal needs. I
I have developed a windows forms c# application, i just want update items in
I just developed an iPhone audio streaming application...Now it can play audio files without
I just developed my first game on Google Android. I'm trying to submit it
I've just developed a large database, and am trying to put it online. I
How can we change body ID while page loading? I have just developed an
I have developed a MVC web application with ASP.NET MVC and im just wondering
I have current a developed app which I am going to submit in just
Im just cross-browser testing my website. It is developed in Asp.net mvc 3. Throughout
I am a little confused about where to get (or create) a distribution profile

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.