There are some significant problems in that html code. The second table row element is missing, and some inputs in the last table row haven’t been closed.
After fixing those, I don’t see any interaction buttons on the page. Looking that the HTML code I see things like btn-rounded so is that bootstrap that you’re using?
But it’s not just bootstrap. The rounded buttons are a part of material design? This is where I feel too many assumptions are being made.
To not delete the last row, you can check if the number of rows is 1 or less and exit out.
To not add more than 10, you can check if the number of rows is 10 or more and exit out.
Can you please give us enough information so that a test page can be put together for your code.
delegated binds so that rows can be added dynamically. I’m assuming CSS is making those seemingly empty items have width and height, probably with an image insertion if i had to guess from the fa-minus indicator. (a minus symbol for deleting a row?)
At a guess (well an educated one, since the OP has openly declared he’s new to JS), the code has been borrowed from a site that was based around wordpress-style declarations for jquery. Probably a pretty old one, given the use of delegate instead of on, as you mentioned.
Yes, you are right, my JS is at step 1 and I am using borrowed code that, while I diod limit my Googling to pages less than a year old, may be outdated.
I “am” learning JS but, in the meantime, I would appreciate a little help with how I can change “what I have here” (as I have invested some time in trying to understand it) just to do those checks.
Right so lets cover a few of the basics of what Paul and I have mentioned, as a learning point:
In a standard webpage context when importing jQuery, $(selector) and jQuery(selector) are equivalant statments, so most people will use $ because it’s shorter.
.delegate was replaced by .on in jQuery 3.X, and deprecated - which means at some point in the future versions of jQuery, it will be removed. You should consider changing to .on for future-proofing, but it won’t cause your code to explode at the current time.
Let’s turn to your code, and what you want to achieve.
Paul’s advice here is pointing you towards making a selection of the rows of your table (Hint: your table body has an ID, and you want the tr’s under it.) and then comparing the number of entries in your selector to 1. If it equals 1, return from the function without doing anything.
(If you get the courage to extend this, consider how you might hide the delete button if there is only 1 row remaining)
Same thing, but in reverse, and inside your add function instead of the delete one: compare the number of rows to 10 (or your variable, either way), and return if it equals.
(The same extension also applies here: try hiding the add buttons if the number of rows is 10.)
Give it a try, show us what you come up with, and we’ll advise further.
not quite sure why it’s adding 1 to that, given that length would be the number of rows… [EDIT: It’s trying to use that as “the next number is”… which makes size a bit of a bad name for that variable… but whatever.]
the selector could be simplified to #tbl_posts_body > tr (or just #tbl_posts_body tr , since there’s no tr’s inside your tr’s…)
So at the start of your add/delete codes, compare that length to the target number (1 or 10, or variable), and if it equals to that number, return;