Jquery problem adding new attribute to form element
<p>Well, I tried reading as much as I could but can't seem to find the specifics for what i need.  I'm new to jquery, but so far it has solved all my previous problems and I'm really liking it.</p>

<p>Here's my problem.  I'm trying to create a dynamic form where the user presses a button and it automatically clones the div object that contains  tags.  Everything works fine, except the delete.  I want to place a delete image inside a span tag so when the user clicks the icon it will remove the appropriate div and its child elements. The cloning, etc. is working fine, and so does the delete (when I just remove the last element), but I need the delete to be executed from the "onclick" event which passes the id of the div and deletes that div...this will allow a user to remove a div element that is in the middle of other elements, etc.  I don't know how to add an onclick event dynamically.</p>

<p>Here is the jquery code:</p>

<pre><code>&lt;script&gt;
$('#btnAdd_expenses').click(function() {
var num     = $('.clonedInput_expenses').length;                
var newNum  = new Number(num + 1);                      
var newElem = $('#expenses_input' + num).clone().attr('id', 'expenses_input' + newNum);
newElem.children(':nth-child(1)').attr('id', 'expenses_label_id' + newNum).attr('name', 'expenses_label' + newNum);

newElem.children(':nth-child(2)').attr('id', 'expenses_value_id' + newNum).attr('name', 'expenses_value\' + newNum);


HERE'S THE PLACE WHERE I'M STRUGGLING:  AT THIS SPOT i HAVE INSERTED THE FOLLOWING CODE WHICH DUPLICATES THE SPAN TAG THAT CONTAINS THE DELETE FUNCTIONALITY, EXCEPT I CAN'T GET THE ONCLICK EVENT TO BE POPULATED...IT'S JUST COPYING THE ONCLICK EVENT FROM THE ELEMENT IN THE MARKUP IT IS CLONING...I WANT IT TO OVERWRITE IT WITH THE FOLLOWING:

newElem.children(':nth-child(3)').attr('id', ' . $id . '_btnDel_expenses' +newNum).addEvent(\'click\', function(){ remove_this(' . $id . 'expenses_input' + num)}); 

$('#expenses_input' + num).after(newElem);
&lt;/script&gt;
</code></pre>

<p>Heres the HTML markup:</p>

<pre><code>&lt;div id="expenses_input1" style="margin-bottom:4px;" class="clonedInput_expenses"&gt;

&lt;input type="text" name="expenses_label1" id="expenses_label_id1" value="Property Taxes" /&gt;&amp;nbsp;

&lt;input type="text" name="expenses_value1" id="expenses_value_id1" value = "$10,000.00" /&gt;&amp;nbsp;

&lt;span title="Remove" id="btnDel_expenses" class="a_hand" onclick="javascript:remove_this(\'' . $id . '_expenses_input1\');"&gt;&lt;img src="../images/delete.png" /&gt;&lt;/span&gt;

&lt;/div&gt;
</code></pre>

<p>I generate this code as a string using php, hence the $id being concatenated with the . but the point is I'm using id's to keep each element_id unique.</p>

<p>Any ideas how to add the onclick event during the cloning of the objects?</p>

<p>Thanks.</p>
