Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are binding the <code>click</code> handler to the elements that are present in the DOM. But, your <code>#removefield</code> element is being dynamically added. So, the event handler is not attached to it.</p> <p>You can use <code>.on()</code> to use event delegation and handle also future elements. Also, you may want to use classnames instead of and <code>id</code> attributes. <code>id</code> attributes need to be unique, but you can set the classname to as many elements as you want.</p> <pre><code>&lt;a href="#" class="removefield"&gt;Remove&lt;/a&gt; </code></pre> <p></p> <pre><code>$("#parts").on("click", ".removefield", function() { /* ... */ }); </code></pre> <hr> <p>The reason why your <code>"Remove"</code> link doesn't work is because you are adding the dynamic <code>&lt;div&gt;</code> element by parts hence making it invalid markup. You should be adding it all together at once. For example,</p> <pre><code>$('#addField').on('click', function () { var part = '&lt;div id="parts' + i + '"&gt;&lt;span&gt;Part&lt;/span&gt; &lt;input type="text" id="auto_part' + i + '" name="auto_part' + i + '" /&gt;&lt;br/&gt;' + '&lt;span&gt;Description&lt;/span&gt; &lt;input type="text" id="auto_description' + i + '" name="auto_description' + i + '" /&gt; &lt;br /&gt;' + '&lt;input type="hidden" id="row_count' + i + '" name="row_count' + i + '" value="" /&gt;' + '&lt;a href="#" class="removefield' + i + '"&gt;Remove&lt;/a&gt;&lt;/div&gt;'; scntDiv.after(part); i++; return false; }); </code></pre> <p></p> <pre><code>$(document).on("click", ".removefield", function() { if( i &gt; 2 ) { $(this).parent('div').remove(); i--; } return false; }); </code></pre> <p><a href="http://jsfiddle.net/YFwrM/" rel="nofollow">You can see it here.</a></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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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