Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><s>You've said</p> <blockquote> <p>i have a table which has trs which all have a "copy" link</p> </blockquote> <p>...but then your code is using</p> <pre><code>$("#menu-item-copyRow")... </code></pre> <p>...to get the copy link. <a href="http://www.w3.org/TR/html5/elements.html#concept-id" rel="nofollow">IDs <strong>cannot</strong> be duplicated</a> within a document, they must be completely unique.</s></p> <p>This is a place where jQuery's new <code>delegate</code> feature comes in <em>really</em> handy (but we can easily emulate it on earlier versions; more on that below). Here's a standalone example (<a href="http://jsbin.com/onave4" rel="nofollow">live copy</a>) of doing this with <code>delegate</code>; I haven't tried to replicate your code per se because I want to avoid complicating things too much, but it should be easy enough to take the technique and apply it to your code:</p> <p>HTML:</p> <pre><code>&lt;table id='theTable'&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;&lt;a href='#' class='copyrow'&gt;Copy&lt;/a&gt;&lt;/td&gt; &lt;td class='descr'&gt;This is a row in the markup&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>JavaScript:</p> <pre><code>jQuery(function($) { var counter = 0; $('#theTable').delegate('a.copyrow', 'click', copyRowClick); function copyRowClick() { var $this, $tr, $clone, $desc, label, n; ++counter; $this = $(this); $tr = $this.parents("tr"); $clone = $tr.clone(); $descr = $clone.find('td.descr'); label = $descr.text(); n = label.indexOf(" of "); if (n &gt;= 0) { label = label.substring(0, n); } label = "Clone #" + counter + " of " + label; $descr.text(label); $clone.insertAfter($tr); return false; } });​ </code></pre> <p>(Ignore the <code>label</code> stuff, it's just so we can tell what's a copy of what.)</p> <p>Because there are no handlers on the elements being copied, we don't have to worry about attaching/detaching those handlers. The only event handler in the above is on the <code>table</code> element.</p> <p><code>delegate</code> is a jQuery 1.4.2+ feature, but with earlier versions you can readily emulate it by hooking the <code>click</code> event on <code>table</code> and then looking at the <code>event.target</code> property to see what was actually clicked. Here's how (<a href="http://jsbin.com/onave4/2" rel="nofollow">live copy</a>):</p> <pre><code>jQuery(function($) { var counter = 0; $('#theTable').click(copyRowClick); function copyRowClick(event) { var $link, $tr, $clone, $desc, label, n; // Is the target an a.copyrow? $link = $(event.target); if ($link.is('a.copyrow')) { // Yes, handle this click -- other than changing the varname // $this to $link, the below is identical to the `delegate` // handler's version. ++counter; $tr = $link.parents("tr"); $clone = $tr.clone(); $descr = $clone.find('td.descr'); label = $descr.text(); n = label.indexOf(" of "); if (n &gt;= 0) { label = label.substring(0, n); } label = "Clone #" + counter + " of " + label; $descr.text(label); $clone.insertAfter($tr); return false; } } });​ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload