Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In case anyone needs an example of the Javascript for dyanmically adding and removing books (JQUERY needed):</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ready(function() { var bookCount=0; $('#btnAddBook').click(function() { bookCount++; //newElem = go up a to the parent div then grab the previous container var newElem = $(this).parent().prev().clone().attr('id', 'book[' + bookCount + ']'); //for each input inside the div, change the index to the latest bookCount $(newElem).find("input").each(function(){ var name = $(this).attr('name'); var leftBracket = name.indexOf("["); var rightBracket = name.indexOf("]"); var beforeBracketString = name.substring(0,leftBracket+1);//+1 to include the bracket var afterBracketString = name.substring(rightBracket); $(this).attr('name', beforeBracketString + bookCount + afterBracketString); }); //insert it at the end of the books $(this).parent().prev().after(newElem); $(newElem).find("input").each(function(){ $(this).attr('id', $(this).attr('id') + bookCount); }); //enable the remove button $('#btnRemovebook').removeAttr('disabled'); //If we are at 16 divs, disable the add button if (bookCount == 15) $(this).attr('disabled','disabled'); }); $('#btnRemoveBook').click(function() { bookCount--; //remove the last book div $(this).parent().prev().remove(); //in case add was disabled, enable it $('#btnAddbook').removeAttr('disabled'); //never let them remove the last book div if (bookCount == 0) $(this).attr('disabled','disabled'); }); }); &lt;/script&gt; &lt;!-- HTML Snippet --&gt; &lt;div id="book[0]"&gt; &lt;label&gt; Book: &lt;/label&gt; &lt;input type="text" name="books[0].author" value="Author" /&gt; &lt;input type="text" name="books[0].title" value="Title" /&gt; &lt;/div&gt; &lt;div&gt; &lt;input type="button" id="btnAddbook" value="Add another book" /&gt; &lt;input type="button" id="btnRemovebook" value="Remove last book" disabled="disabled" /&gt; &lt;/div&gt; &lt;!-- REST of the HTML --&gt; </code></pre>
 

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