Limit number of dynamically added Table Rows using JQuery -
I am dynamically inserting a row with JQuery in a table by cloning it.
$ ('#clone'). Click (function () {// this reference has been placed in a var var $ btn = $ (this). Use parent (); // cloning to navigate to the nearest line from the .closest () buttons. Use Clone (true) to pass the events for clone items var $ clonedRow = $ btn.closest ('tr'). Clone (true) .inertAfter ($ btn);});
The end user will control the inclusion of new rows. I need to limit the number of new rows to 5. There is a way to do this with any cookie or any other method (array). I can have more than one table, which has its own unique IDs, so it has to work with several tables on the page.
Hope you can help.
You can always track the number of additional rows by using one line for each table.
Or any other option is to use the jQuery data () method which allows you to store data directly in the DOM element Click Event will find its table, and every time a row is added to the data in the table (), And to stop it when reaching the maximum maximum.
Edit: Check the correct code to see if the contract is undefined, and to remove the brackets placed in the wrong place.
$ ('#clone'). Click (function () {var $ btn = $ (this) data ('the code') == undefined) {$ btn.closest ('table'). Data ('the count', 0)} var currentCount = $ Btn.closest ('table'). Data ('thecount'); If (current count <5) {$ btn.closest ('tr'). Clone (true) .inertAfter ($ btn); $ Btn.closest ('Table'). Data ('the number', presentcount + 1);}});
Comments
Post a Comment