Note that there are some explanatory texts on larger screens.

plurals
  1. POAllow the user to add form input fields: Why is append() only working once?
    primarykey
    data
    text
    <p>I'm trying to create an order form that has an "add new product" section within a table. Basically the user should be able to click on the "Add Row" link and a new input line will appear to allow the user to add another product to the order list. I'm able to have this work once, but after that the form won't add any new lines. In the console.log I can see that the event handler is only working on the original form element, not the new product input line. I have a shortened fiddle of what I'm working with here:</p> <p><a href="http://jsfiddle.net/scottm1164/eTBr6/5/" rel="nofollow">http://jsfiddle.net/scottm1164/eTBr6/5/</a></p> <p>Here is my jQuery:</p> <pre class="lang-js prettyprint-override"><code>&lt;script&gt; $(document).ready(function () { (function () { var start = $('table'), newRow = $('&lt;tr&gt;&lt;td&gt;&lt;input type="text" class="quantity" /&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" class="prodNum" /&gt;&lt;/td&gt;&lt;td&gt;&lt;textarea cols="15" rows="1"&gt;&lt;/textarea&gt;&lt;/td&gt;&lt;td&gt;&lt;p class="addRow"&gt;Add a Row&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;'); $('.addRow').on('click', function () { $(start).append(newRow); console.log(this); }); })(); //end SIAF }); //END document ready &lt;/script&gt; </code></pre> <p>Here is my Form:</p> <pre class="lang-html prettyprint-override"><code>&lt;form enctype="multipart/form-data" method="post" action="#" id="orderForm"&gt; &lt;ul&gt; &lt;li&gt;Name: &lt;br&gt; &lt;input type="text" id="name" /&gt; &lt;/li&gt; &lt;li&gt;Email: &lt;br&gt; &lt;input type="email" id="email" /&gt; &lt;/li&gt; &lt;li&gt;Company: &lt;br&gt; &lt;input type="text" id="company" /&gt; &lt;/li&gt; &lt;li&gt;Phone: &lt;br&gt; &lt;input type="tel" id="phone" /&gt; &lt;/li&gt; &lt;li&gt;Delivery Address: &lt;br&gt; &lt;input type="text" id="delAddress" /&gt; &lt;/li&gt; &lt;li&gt;PO Number: &lt;br&gt; &lt;input type="text" id="poNum" /&gt; &lt;/li&gt; &lt;li&gt;If you have a document for your order, attach it here: &lt;br&gt; &lt;input type="file" id="docs" /&gt; &lt;/li&gt; &lt;li&gt; &lt;table id="prodTable" width="auto" border="0" cellpadding="4" cellspacing="4"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;Quantity&lt;/td&gt; &lt;td&gt;Product Number&lt;/td&gt; &lt;td&gt;Product Description&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;input type="text" class="quantity" name="quantity_" /&gt; &lt;/td&gt; &lt;td&gt; &lt;input type="text" class="prodNum" name="prodNum_" /&gt; &lt;/td&gt; &lt;td&gt; &lt;textarea cols="15" rows="1" name="prodDesc_"&gt;&lt;/textarea&gt; &lt;/td&gt; &lt;td&gt; &lt;p class="addRow"&gt;Add a Row&lt;/p&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/li&gt; &lt;li&gt;Special Delivery Instructions: &lt;br&gt; &lt;textarea cols="30" rows="10" id="instructions"&gt;&lt;/textarea&gt; &lt;/li&gt; &lt;/ul&gt; &lt;input type="submit" id="submit" /&gt; &lt;/form&gt; </code></pre> <p>Can somebody explain to me what is going wrong with this code, I'm fairly new to jQuery/Javascript so I just don't understand why the code only works once, but not after subsequent clicks to the "add a row" link, and only on the original "add a row" links but not on the newly created link.</p>
    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.
 

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