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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:28:17+00:00 2026-05-26T04:28:17+00:00

Everyone is suggesting some framework, but I’d like to know how it can be

  • 0

Everyone is suggesting some framework, but I’d like to know how it can be done in native JavaScript. I’ve tried this code and some other things, no effect. Seems that I’m unaware of some basic underlying concept. Any help would be appreciated.

window.onload = function() {
    var trCurrent
    var main = document.getElementById('main');
    var tr = main.getElementsByTagName('tr');
    function hl() {
            trCurrent.setAttribute('class', 'highlight');
    };
    for (var x = 0; x < tr.lenght; x++) { 
        trCurrent = tr[x];
        trCurrent.addEventListener ('mousedown', hl, false);
    }
};
  • 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-26T04:28:18+00:00Added an answer on May 26, 2026 at 4:28 am
    • You have to change trCurrent to this at your function h1, becayse trCurrent points to the last defined TR element (trCurrent = tr[x] for a high x).
    • Use .length instead of .lenght.

    Final code:

    window.onload = function() {
        var main = document.getElementById('main');
        var tr = main.getElementsByTagName('tr');
        function hl() {
            this.setAttribute('class', 'highlight');
        };
        for (var x = 0; x < tr.length; x++) { 
            tr[x].addEventListener ('mousedown', hl, false);
        }
    };
    

    If you want to use an variable which is subject to change during a loop, it’s required to wrap the body in an (anonymous) function:

    window.onload = function() {
        var main = document.getElementById('main');
        var tr = main.getElementsByTagName('tr');
        for (var x = 0; x < tr.length; x++) {
    
            (function(trCurrent){ //Anonymous wrapper, first argument: `trCurrent`
                function hl() {
                    trCurrent.setAttribute('class', 'highlight');
                };
                trCurrent.addEventListener ('mousedown', hl, false);
            })(tr[x]); //Passing a reference to `tr[x]`
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Everyone uses source-code control to manage versions (right?) and this provides some level of
I know there are some questions on here that are very much like this
I know there are already several similar questions here, but I need some recommendations
I'm face to face with this issue, and I don't know how I can
I have a framework which works this way: After defining some meta data, it
Obviously, horses for courses, but what are some good ways to integrate javascript libraries
I have a some elements, roughly like this: <div> <a> <div> When a user
I created a some javascript/jquery code to filter an html table. The filter works
Everyone has accidentally forgotten the WHERE clause on a DELETE query and blasted some
Everyone (at least everyone who uses a compiled language) has faced compilation errors but

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.