What is the best way to remove a specific child row in jQuery datatables?

I currently have two static default rows and I would like to add more rows below the existing ones without deleting the parent rows. However, I am unsure of how to delete child rows without affecting the parent rows.

My parent rows consist of the default two rows which I do not want to delete. My aim is to only delete the child rows.

var counter = 1;
var FieldCount = 1;
$('#cashTable').on('click', '.button-add', function() {
  ctr++;
  var cashacc_code = 'cashacc_code' + ctr;
  var cashacc = 'cashacc' + ctr;
  var cash_narrat = 'cash_narrat' + ctr;
  var cashdeb = 'cashdeb' + ctr;
  var cashcredit = 'cashcredit' + ctr;
  var newTr = '<tr class="jsrow"><td><input type="number" class=' + "joe" + ' id=' + cashacc_code + ' placeholder="NNNN" /></td><td><select class="form-control" id="cashacc" ><option value="">Choose an Items</option><option value="1">Joe</option><option value="2">Joe</option><option value="3">Joe</option></select></td><td><input type="text" class=' + "joe" + ' id=' + cash_narrat + ' placeholder="Enter Here" /></td><td><input type="number" class=' + "joe" + ' id=' + cashdeb + ' ...
  
  $('#cashTable').append(newTr);
 
  $(document).ready(function() {
    $('.dlt-icon').click(DeleteRow);
  });

  function DeleteRow() {
    $(this).parents('tr').first().remove();
  }
});
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<table id="cashTable" class="table table-bordered table-striped" required>
  <thead>
    <tr>
      <th>A/c Code</th>
      <th>Account Name*</th>
      &...

    <!-- Second Row -->

    <tr id="sndRow">
      <td>
        <input type="number" class="form-control" id="rowNum" name="cashaccCode" placeholder="NNNN" />
      </td>
     
      ...

    </tr>

  </tbody>


If you are having trouble understanding this, please let me know.

Answer №1

To prevent the deletion of the first two rows that were already created, you can utilize .jsrow as shown below:

It’s important to note the correct usage of classes. For instance, you included the class button-add in both the add and remove buttons. Refer to my code snippet for the appropriate correction:

var ctr = 1;
var FieldCount = 1;
$('#cashTable').on('click', '.button-add', function() {
  ctr++;
  var cashacc_code = 'cashacc_code' + ctr;
  var cashacc = 'cashacc' + ctr;
  var cash_narrat = 'cash_narrat' + ctr;
  var cashdeb = 'cashdeb' + ctr;
  var cashcredit = 'cashcredit' + ctr;
  // The rest of your JavaScript code here
});

$(document).on( 'click', '.dlt-icon', function() {
    $(this).parents('tr.jsrow').first().remove();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="cashTable" class="table table-bordered table-striped" required>
  <thead>
    <tr>
      <th>A/c Code</th>
      <th>Account Name*</th>
      <th>Narration*</th>
      <th>Debit*</th>
      <th>Credit</th>
    </tr>
  </thead>
  <tbody>
    <tr id="fst_row">
      <!-- First row -->
      <td>
        <input type="number" id="cashacc_code" placeholder="NNNN" class="form-control" name="cashacc_code" />
      </td>
      // Rest of the HTML content
    </tr>

    <!-- Second Row -->

    <tr id="sndRow">
      <td>
        <input type="number" class="form-control" id="rowNum" name="cashaccCode" placeholder="NNNN" />
      </td>
      // Further HTML content
    </tr>

  </tbody>
</table>

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

Adjust the spacing of a div based on the fluctuating height of another dynamically changing div

Is it possible to dynamically adjust the margin of a div using jQuery or JS? Currently, I have set a margin on a div by calculating the height of a container that includes images. var articleImageHeight = $('.slides_control').height(); $(' ...

Decoding JavaScript elements embedded in an HTML website

My current HTML site includes the following code: <script type="text/javascript"> jwplayer('player_container').setup( { 'width': '640', ...

What is the best way to trigger an ajax request when a user selects a tab?

How can I trigger an ajax call when a tab is clicked by the user? What is the best way to handle the HTML response and display it within the tab? How do I bind JavaScript events to the dynamically loaded HTML content? I am familiar with using jQueryUI tab ...

What is the best way to create rounded corners on a WordPress login page?

I recently implemented the Custom Login Page Customizer plugin on my WordPress website and it has created a beautiful login page. While I was able to round the corners of my logo using CSS, I'm struggling to achieve the same effect on the login form. ...

Can you place more than one Twitter Bootstrap carousel on a single webpage?

Latest Version of Twitter Bootstrap: 2.0.3 Sample HTML Code: <!DOCTYPE html> <html dir="ltr" lang="en-US" xmlns:og="http://opengraphprotocol.org/schema/"> <head> <link rel="stylesheet" type="text/css" media="all" href="reddlec/style. ...

What is the best way to dynamically retrieve a URL and utilize it as a hyperlink using jQuery?

Is there a way to retrieve the current URL and use it as a link in jQuery? For example, if my browser is currently on page mysite.com/index.php?page=post&see=11, I would like to use this URL in my link and append /new/ after the domain name, like so: ...

Using Axios in a VUE application to handle response data

I've been working on a VUE application and I'm currently exploring how to handle post responses with Axios. Initially, I used vue-router for fetching data but decided to give Axios a try. Axios code: methods: { sendForm () { ...

What are all the functionalities provided by jquery select?

Currently, I am venturing into testing out a new jQuery plugin. let myPlugin = new MyPlugin(); $(#myPlugin).someFunction(); console.log($(myPlugin)) I'm curious - is there a way for me to see a full list of the available functions/methods for me to ...

Implementing MVC2 AJAX to dynamically set the UpdateTargetId depending on the response data

The situation: I'm currently developing a login form for an MVC2 application. My approach: The form is set up to submit to an MVC2 action that verifies the username and password. If the validation fails, the action returns the form (a partial view) ...

Extract from Document File

After receiving a PDF through an Angular Http request from an external API with Content Type: application/pdf, I need to convert it into a Blob object. However, the conventional methods like let blobFile = new Blob(result) or let blobFile = new Blob([resul ...

I'm experiencing an issue where the Devtool appears empty when inspecting it, making it impossible to locate

I'm currently working on automating the process of entering data into web forms using Selenium VBA. The forms are located on our organization's SharePoint site, and I have already logged into the account. However, every time I reach the final ste ...

There was a dependency resolution error encountered when resolving the following: [email protected] 12:15:56 AM: npm ERR! Discovered: [email protected] . The Netlify deploy log is provided below for reference

5:27:42 PM: Installing npm packages using npm version 8.19.3 5:27:44 PM: npm ERR! code ERESOLVE 5:27:44 PM: npm ERR! ERESOLVE could not resolve 5:27:44 PM: npm ERR! 5:27:44 PM: npm ERR! While resolving: [email protected] 5:27:44 PM: npm ERR! Foun ...

Tips for matching the height of a child div to its parent's height:

Is there a way to ensure that the height of the child div matches the height of the parent div without explicitly setting it in the CSS? Even when the parent div has a height of 300px, the child divs car1front and card1back do not seem to adjust accordingl ...

Utilizing React Material UI for form validation with error handling and informative helper text

I recently created a form in which I needed to validate the inputs. During my research, I came across the material UI library that has an attribute called error boolean, and another attribute called helperText for the TextField input of the form. However ...

Accessing variable from JavaScript function in Python App Engine

Embarking on my first journey into the world of web technologies, I find myself tangled in a dilemma. Currently immersed in my initial appengine project, I am attempting to manipulate a value generated within a JS function (embedded in my .html file) using ...

Defining the dimensions of a Material Dialog box in Angular: A step-by-step guide

Thank you for taking the time to read this. I'm currently trying to create a dialog box with a specific size of 90% in width, but I'm facing some challenges in achieving this. Can anyone guide me in the right direction or just provide me with th ...

Retrieve the outcome of an AJAX request in JavaScript within a separate function

My uploadFunction allows me to upload files to my server using a Rest web service called with JQuery ajax. I also use this method for another function and need to determine the result of the web service call in order to add a row with the name of the uploa ...

Stop the Decimal in MUI TextField

Having trouble creating a customized Currency Input? You may be experiencing an issue where the textfield prevents the input of decimals. Take a look at the code below for possible solutions. main.ts const [_val, setVal] = React.useState(""); const h ...

What is the best way to prompt users to submit comments with a popup textarea box?

Within my project, I have incorporated a dropdown list: <div class="dropdown"> <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Select subject <span class="caret"></span> </but ...

"Implementing the Fetch GET method with parameters in React.js - a step-by-step guide

I'm interested in utilizing the Ajax command within a Fetch command in React JS. var data = { resource_id: '5f5afc43-639a-4216-8286-d146a8e048fe', // the resource id }; fetch('https://data.gov.il/api/action ...