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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:58:23+00:00 2026-06-16T21:58:23+00:00

I am trying to setup a click event to a <a> tag inside a

  • 0

I am trying to setup a click event to a <a> tag inside a TD

I have

test.prototype.buildData = function() {
 cell = createElement('td', {innerHTML: "<a class='link' onclick='"+this.edit(this)+"'>"+ value + "</a>"});
 this.table.appendChild(cell);

 //many cells..all need to attached the click event

}

test.prototype.edit=function(this){
  this.style.backgroundColor='red'  
}

I want to modify the clicked cell background color. I also need to register click event ONLY to the <a> tag. I know my this.edit(this) doesn’t make sense.

Are there anyways to do this? Thanks a lot!

  • 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-16T21:58:24+00:00Added an answer on June 16, 2026 at 9:58 pm

    Try something along these lines…

    test.prototype.buildData = function (value) {
        var cell = document.createElement("td"),
            anchor = document.createElement("a");
    
        anchor.className = "link";
        anchor.addEventListener("click", (function (self) {
            return function () {
                self.edit(this);
            };
        })(this), false);
        anchor.innerHTML = value;
    
        cell.appendChild(anchor);
    
        this.table.appendChild(cell);
    };
    
    test.prototype.edit = function (el) {
        el.style.backgroundColor = "red";
    };
    

    NOTES:

    1. When you assign a function as an event handler via the addEventListener method, the value of this within the function is the DOM element that triggered the event.
    2. The second argument for addEventListener is a function which is simply an object like everything else in JavaScript. Therefore, you can use an immediately invoked function expression that returns a function that would contain the actual event handling code.
    3. The value of this can be tricky if you’re new to JavaScript. If you look at my IIFE which is the function defined inside the parentheses right after the “click” argument for the addEventListener method you’ll note that I’m passing this in as an argument to that at the end (right before the false argument). What I’m doing here is passing the value of this from the perspective of the buildData method which equates to test.prototype. The IIFE sees that as the self argument, though, so in the returned function it calls self‘s (i.e. test.prototype) edit method with the argument this which is in this case the element that triggered the event.
    4. test.prototype.edit takes an element as its single argument and changes the background color.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to setup a click event on a div, and I would like
I am trying to setup some click events inside a jquery plugin. The plugin
I'm trying to add an event to my backbone view I have setup. I
I am trying to setup an event function to react on clicked links in
I have been trying to setup git for our web development team unsuccessfully. Some
I want to setup a click event trigger in jQuery for certain anchor tags.
I have a back button setup on my Show Event page and its simply:
I was trying to setup this when you click outside of the element, close
I have a UserControl that I am trying to hook up event handlers from
I'm trying to setup the validval plugin (latest 4.0.0) version and have no luck

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.