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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:43:21+00:00 2026-05-20T23:43:21+00:00

I am using the jQuery clone() method to clone a table row, then using

  • 0

I am using the jQuery clone() method to clone a table row, then using the attr() method to change the name/id of some of the fields. This all works well and good and if I use "view generated source" the names/IDs are getting set properly however when the generated content triggers a JavaScript event it triggers it as if it were the parent element. For example I have an element with the name/id ‘slast1’ which should trigger the autocomplete method (based on the fact that it is part of the ‘slast’ class) and that works fine. Unfortunately when I click the same element in the generated row (in this case the element id/name is ‘slast2’) it triggers the autocomplete method but acts as if it was triggered from the parent element, ‘slast1’. I can’t seem to find a way around this and am thinking it might be a limitation of using the clone() method but am hoping I am just missing something.

function fnAddRow(iRow)
{
  var iNewRow = iRow+1;
  $("#tblAuthors tr:last").clone(true).insertAfter("#tblAuthors tr:last");
  $("#tblAuthors tr:last #slast"+iRow).attr("name", "slast"+iNewRow);
  $("#tblAuthors tr:last #slast"+iRow).attr("id", "slast"+iNewRow);
  $("#slast"+iNewRow).val("");
}

The above code clones the table row and changes the attributes as I mentioned before. The code below is what gets triggered by the keydown event in the slastX element but for whatever reason the generated elements are treated as if they were the parent.

  $(function()
  {
    var aEmps = 
    [
<?php
echo $sEmps;
?>    
    ];

    $(".slast").bind( "keydown", function( event ) {
      if ( event.keyCode === $.ui.keyCode.TAB &&
          $( this ).data( "autocomplete" ).menu.active ) 
          { event.preventDefault();}
    })
    .autocomplete({
      source: aEmps,
      close: function(event, ui)
      {
        // Split return value and store in array
        var aName = this.value.split(", ");
        // Get row #
        var iRow = this.name[this.name.length-1];
        // Populate values        
        $("#slast"+iRow).val(aName[0]);
        $("#sfirst"+iRow).val(aName[1]);
        $("#smi"+iRow).val(aName[2]);
      }
    });
  });

As you can see the autocomplete method should be triggered by anything with the slast class but for reasons I can’t figure out $(this) always = the parent ('slast1') as opposed to slast2 or any other generated content.

  • 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-20T23:43:22+00:00Added an answer on May 20, 2026 at 11:43 pm

    When you are using .clone(true) you are coping all data() and all events bound to that element. Which is causing your problems.

    If you switch your event binding to use .delegate() (or .live()) you should be able to fix your problem of the event being triggered.

    var autoCompleteOptions = {
      source: aEmps,
      close: function(event, ui)
      {
        // Split return value and store in array
        var aName = this.value.split(", ");
        // Get row #
        var iRow = this.name[this.name.length-1];
        // Populate values        
        $("#slast"+iRow).val(aName[0]);
        $("#sfirst"+iRow).val(aName[1]);
        $("#smi"+iRow).val(aName[2]);
      }
    };
    
    $("#tblAuthors").delegate("tr .slast", "keydown", function(event){
        if ( event.keyCode === $.ui.keyCode.TAB && 
              $( this ).data( "autocomplete" ).menu.active ) { 
         event.preventDefault();
       }
    }).autocomplete(autoCompleteOptions);
    
    
    function fnAddRow(iRow)
    {
      //....stuff.....
      $("#tblAuthors tr:last #slast"+iRow).autocomplete(autoCompleteOptions);
      //....stuff.....
    }
    

    With using delegate you are binding the event to $("#tblAuthors") and not the .slast directly.

    Also on a side note, using delegeate might be able to gain you a bit of performance as well in relation to this answer.

    UPDATE: Mark and I went back-and-forth a lot on the solution to this, here is the final code: http://jsfiddle.net/niczak/HjCMh/1/

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

Sidebar

Related Questions

I am dynamically inserting a row into a table with JQuery using clone. $('#Clone').click(function()
Using jQuery, how do I delete all rows in a table except the first?
Using jQuery, how do you bind a click event to a table cell (below,
Using jQuery, is there a way to select all tag that reference a specific
When using jQuery 's ajax method to submit form data, what is the best
I'm using jQuery and SimpleModal in an ASP.Net project to make some nice dialogs
I am using a jquery tablesorter plugin( http://tablesorter.com/docs/ ) for sorting my tables. This
This is my code : var a=[1,2,3] b=$.clone(a) alert(b) Doesn't jQuery have a 'clone'
Using jQuery , how can I dynamically set the size attribute of a select
Using jQuery, how do you check if there is an option selected in a

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.