The dynamic ID stops functioning once a row is deleted from the table

I have a table with feeSetting id containing dynamic data. I am able to add and remove rows from the table. The issue arises when a row is removed and a new row is added, as it overrides the unique ID of the last row. I need the table to generate unique IDs while still maintaining the dynamic add and remove functionality. Below is the code snippet for adding a row:

<table id="feeSetting">
<tbody>
</tbody>
</table>

<script>
     function AddRow() {
            debugger;
            var index = 0;
            if ($("#feeSetting tbody tr").length > 0) {
                  index = $("#feeSetting tbody tr").length;
            }

            $("#feeSetting tbody").append("<tr class='gradeX'>"
                + "<td class='col-md-3'><input type='text' value='' class='form-control validate[required,custom[number]] text-input txtFromDay' id='eFromDay'/></td>"
                + "<td class='col-md-3'><input type='text' class='form-control validate[required,custom[number],min[1]] text-input txtValue' value='' id='eValue-" + index + "'/></td>"
                + "<td class='col-md-4'>"
                + "<div id='loadTypes-" + index + "'  class='typeValidation'></div></td>"
                + "<td class='col-md-2'><input type='button' class='btn btn-danger btn-sm' value='Remove'/></td>"
                + "</tr>");
            renderPartialInDiv('@Url.Action("GetValidationTypeDropDown", "FeeFineSetting")?strDDName=eValidationTypeList-' + index + '&intDDID=0&intValidationID=1', '#loadTypes-' + index);
            $('#eValidationTypeList-'+index).select2();
        };
</script>

Answer №1

Consider implementing a global variable that increases its value with the addition of each new row. Here is an example code snippet:

<tablen id="feeSetting ">
<tbody>
</tbody>
</table>

<script>
    //declare this variable outside the function to make it global
     var index = 0;
     function AddRow() {
            debugger;
            index++;
            $("#feeSetting tbody").append("<tr class='gradeX'>"
                + "<td class='col-md-3'><input type='text' value='' class='form-control validate[required,custom[number]] text-input txtFromDay' id='eFromDay'/></td>"
                + "<td class='col-md-3'><input type='text' class='form-control validate[required,custom[number],min[1]] text-input txtValue' value='' id='eValue-" + index + "'/></td>"
                + "<td class='col-md-4'>"
                + "<div id='loadTypes-" + index + "' class='typeValidation'></div></td>"
                + "<td class='col-md-2'><input type='button' class='btn btn-danger btn-sm' value='  Remove '/></td>"
                + "</tr>");
            renderPartialInDiv('@Url.Action("GetValidationTypeDropDown", "FeeFineSetting")?strDDName=eValidationTypeList-' + index + '&intDDID=0&intValidationID=1', '#loadTypes-' + index);
            $('#eValidationTypeList-'+index).select2();
        };
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Simultaneously transitioning two full-width elements by sliding them in and out

Currently, I am working on creating an animation effect where one element slides out of the screen on the left while fading out to 0 opacity. Simultaneously, a new element slides in from the right to take its place. Although the visual aspect is functioni ...

Connect two cells in a table by incorporating a vertical line in between the rows. Merge header cells

I have incorporated some simple HTML tables on my website and I am encountering two issues that I need assistance with. 1) After adding these tables to my subpage, I noticed that the tables look almost identical but have their horizontal line between tab ...

Enhancing Error Validation Messages in FormControl to Include More Specific Details

In continuation of my previous inquiry, my objective is to display a customized error message for user inputs. Instead of presenting users with a generic error message, I want to provide them with specific feedback. For example, if the input field only al ...

Attach an event listener to a particular textarea element

Currently, I am developing a project in Next.js13 and my focus is on creating a custom textarea component. The goal is to have this component add an event listener to itself for auto-adjusting its height as the user types. Below is the relevant section of ...

We have encountered an issue with the syntax in the code: ajaxsample/update_agenda. It seems to be an unrecognized expression according to

Here's the code for updating my controller: public function update_agenda() { $id= $this->input->post('did'); $this->load->model('agenda_model'); $data = array ( ...

PHP table not displaying data

I am brand new to this and currently enrolled in a class with an unhelpful professor. I am attempting to read data from a file into a table, but for some reason the data is not displaying. I have tried multiple approaches, but I am feeling quite lost. Any ...

Avoid displaying pop-up repeatedly (using current library)

I am currently customizing a Shopify theme that includes its own popup settings. I have created a new popup for my website that prompts visitors to choose their preferred language upon arrival. Everything seems to be working fine up to this point, but the ...

Observable<void> fails to trigger the subscriber

I am currently facing a challenge with implementing a unit test for an Observable in order to signal the completion of a process. While there is no asynchronous code in the logout function yet, I plan to include it once the full logic is implemented. The m ...

Button for Selecting Colors

I love the color picker button on Google Keep, where a variety of colors pop up for selection. I'd like to add something similar to my website. What steps should I take to implement this feature? ...

Calculate the total value of the checkboxes that are either selected or unselected, and display the result

I am currently working on a project that involves checkboxes within labels. I need to extract the values of the selected checkboxes, calculate their sum, and then display the total in a Div below. The total will be updated dynamically as checkboxes are se ...

Adjusting the X-axis in Highstock: Tips and Tricks

Is there a way to adjust the X axis on this chart? My goal is to shift it so that it only covers half of the width, leaving the other half blank for future plotlines. Any suggestions on how to achieve this? Thanks! :) ...

How can you ensure a script runs only once another script has completed its execution?

Consider the following code snippet I created to illustrate my idea: var extract = require("./postextract.js"); var rescore = require("./standardaddress.js"); RunFunc(); function RunFunc() { extract.Start(); console.log("Extraction complete"); ...

What steps can we take to create a personalized PDF editor incorporating our unique edit functionalities using Vue.js?

Is it possible to create a PDF editor similar to MS Word using vuejs? How can I implement custom logic in the PDF editor with vuejs? For example, the features should include: Conditional replacement of text Adding tags to text within the PDF Changing the ...

The header logo will have an absolute position to overlay the navigation menu when the screen is resized

I am currently working on a website layout that involves using position: absolute for the logo to overlay another div below it, purely for stylistic reasons. While I have achieved this effect, it doesn't translate well into responsive web design. When ...

Steps to incorporate padding to a nested Angular Component by leveraging the CSS of its parent component

It is evident from the example at https://material.angular.io/components/expansion/examples that material components can be customized using the CSS of the component embedding them: .example-headers-align .mat-form-field + .mat-form-field { margin-left: ...

Error: Module not found - Imported Node Module

I have been struggling to make modifications to an npm package with little success. I have spent hours troubleshooting and would greatly appreciate some guidance. https://www.npmjs.com/package/dxf After forking the package into my Github account, I proce ...

What is the best way to clear a form and eliminate all validation errors?

My Angular form includes fields validated using the ng-pattern attribute, along with a reset button. I utilize the Ui.Utils Event Binder to manage the reset event as follows: <form name="searchForm" id="searchForm" ui-event="{reset: 'reset(searchF ...

What is the appropriate way to add left padding or left margin to the modal footer in Bootstrap 5.3

I have been working on creating a modal that resembles a chat box. I have completed most of the work, but I am facing an issue with the input area not being fixed. My goal is to have the input text displayed on the footer of the modal, but it seems like th ...

Storing extensive JSON data with AJAX, jQuery, and Java

Currently, I am utilizing jQuery AJAX to call a server-side method and sending a JSON string to the controller. Everything works smoothly when the JSON size is small, but as soon as it exceeds 7kb, the server side rejects the JSON string. I suspect that t ...

Angular directive ng-template serves as a component type

In the code snippet below, <!DOCTYPE html> <html> <head> ..... </head> <body ng-app="app"> <script type="text/ng-template" id="my-info-msg.html"> <s ...