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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:05:35+00:00 2026-05-30T19:05:35+00:00

I have multiple function objects declared from the same function that deal with their

  • 0

I have multiple function objects declared from the same function that deal with their own tables with dynamically added rows. I need to have a delete button of some kind for each row. Since there can be multiple of these objects, the row must know what object it is a part of, and the object could be nested in some arbitrary scope inside other objects (and out of reach of some arbitrary function declaration, I think?), the typical solution of an onClick referencing some other javascript function doesn’t seem sufficient. I’ve got a setup using delegate to catch the click, but I’m not sure where to go from there. Here’s some example code:

function testobject() {
 //other stuff

 $stateadd.click(function() {
  if ($stateselect.val() in states) { //already in array

  } else {
   $temp = $("<tr><td nowrap>" + USStates[$stateselect.val()].Name + "</td><td><input type=\"button\" class=\"testthing\" value=\"Remove\" /></td>").appendTo($selectedstates);
   $temp2 = $temp.find('.testthing');

   $temp.delegate($temp2, 'click', function(e) {
    //how to know what row to delete?  how to know which object the row is in?
    alert($(e.target).attr('class')); //returns proper class, so I can get the button object itself
   });

   states[$stateselect.val()] = $temp;
  }
 });

 //other stuff
}

To be clearer, I need two things in the click event: the testobject instance that made the row, and the $stateselect.val() id associated with that row. I can’t just delete the row tr/td tags, I need to have it removed from the states object as well.

  • 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-30T19:05:36+00:00Added an answer on May 30, 2026 at 7:05 pm

    Use this to refer to the element which is clicked and then you can find the corresponding parent tr element using jQuery closest() method.

    Also the first argument of delegate is the selector on which the delegated event should work. You were passing a jQuery object so it didn’t work. Try this.

    $('tableSelector').delegate('.testthing', 'click', function(e) {
        //Here "this" will point to the remove button.
    
        var $tr = $(this).closest('tr');//this will give you the corresponding row  
    });
    

    jQuery closest() gets the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.

    If you are using latest jQuery ver(1.7+) you can use on method.

    $('tableSelector').on('click', '.testthing', function(e) {
        //Here "this" will point to the remove button.
    
        var $tr = $(this).closest('tr');//this will give you the corresponding row  
    });
    

    Note: You should keep this code outside testobject method because you don’t need to add delegate or on everytime you call testobject method.

    Update: Based on the comments

    Add a data attribute to the input remove button with stateselect value.

    $temp = $('<tr><td nowrap>' + USStates[$stateselect.val()].Name + '</td><td><input type="button" data-stateid="' + $stateselect.val() + '" class="testthing" value="Remove" /></td>').appendTo($selectedstates);
    

    Js change

    $('tableSelector').delegate('.testthing', 'click', function(e) {
        //Here "this" will point to the remove button.
    
        var sateId = $(this).data('sateid');
        var $tr = $(this).closest('tr');//this will give you the corresponding row  
    
        //Here you can access states[stateId] and do what you want
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple data transfer objects that need their data transferred over to their
I have a function in R that I call multiple times. I want to
I have a function that uses out parameters to return multiple values to the
Say I have a Python function that returns multiple values in a tuple: def
I have an AS3 program that calls a function multiple times. The function must
In my code I have multiple objects being added to the repository, I have
I have a group of multiple objects. If I select one object from the
I have multiple setTimeout functions like this: function bigtomedium(visiblespan) { visiblespan.removeClass('big').addClass('medium'); setTimeout(function(){ mediumtosmall(visiblespan);},150); };
I have multiple document.ready functions on a page and I want a function to
In python, you can have a function return multiple values. Here's a contrived example:

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.