Home  >  Article  >  Web Front-end  >  How to add, edit and delete table rows in jQuery?

How to add, edit and delete table rows in jQuery?

王林
王林forward
2023-09-05 21:49:10850browse

How to add, edit and delete table rows in jQuery?

In today's era of web development, effective and efficient table management has become very important, especially when dealing with data-heavy web applications. The ability to dynamically add, edit, and delete rows from a table can significantly enhance the user experience and make applications more interactive. One effective way to achieve this is to leverage the power of jQuery. jQuery provides many features to help developers perform operations.

Table row

A table row is a collection of interrelated data, represented by the

element in HTML. It is used to group cells (represented by elements) together in a table. Each element is used to define a row in the table. For multi-attribute tables, it usually contains one or more elements.

grammar

$(selector).append(content)

The append() method in jQuery is used to add content to the end of an element, whether it is a single element or a group of elements. Content can be text, HTML, or other elements.

The content can be an HTML string, a DOM element or a jQuery object.

$(selector).find(selectCondition)

The find() function in jQuery is used to search and select descendant elements of the selected element. It searches the entire DOM tree for the selected element and returns all matching elements in a new jQuery object.

Where selectCondition is a string representing the element to be selected, such as a class or tag name.

$(selctor).remove()

The remove() method in jQuery is used to remove the selected element and its child elements from the DOM (Document Object Model).

method

In order to perform operations on table rows using jQuery, we will use a combination of jQuery methods and DOM manipulation techniques. Let us discuss all the three operations that we will see in this article namely adding, editing and deleting rows separately. So, talking about the first operation i.e. adding a new row to the table, we will use the append() method mentioned above. To do this, first we will build a new

element and then insert it into the table using the append() method.

We will then fill the new row with the new

element and assign it a value using the text() or html() method. Now for the second operation, which is to modify the table row, we first use the find() method to select the relevant elements in the row, and then use the text() or html() method to modify their content. Finally, to perform the final operation, which is to delete the table row, we simply use the remove() method on the row we want to delete.

But before we start explaining all the parts, first I want to draw your attention to the fact that in order to include jQuery into my web pages, I use a CDN, which you can see in the script tag.

Now back to the code, let’s discuss the body element first. The body contains a virtual table with two rows and two columns. Apart from this, it also contains three buttons labeled "Add Row", "Delete Row" and "Edit Row" which we will use later in the script to add the required functionality to the code. I also used CSS to add some styling to improve the appearance of the table rows. Finally in the scripting part, I wrote the logic for all the functions using jQuery. I utilized all the above functions to accomplish this functionality. This functionality has been implemented using jQuery event handlers, which are fired when the corresponding button is clicked.

Example

Here is the complete code we will use in this example -

<!DOCTYPE html>
<html>
<head>
   <title>How to Add Edit and Delete Table Row in jQuery?</title>
   <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
   <style>
      table{
         border: 2px solid black
      }
      td{
         padding: 2px
      }
   </style>
</head>
<body>
   <h4>How to Add Edit and Delete Table Row in jQuery? </h4>
   <table id="my-table">
      <tr>
         <td>Original Row</td>
         <td>Data</td>
      </tr>
      <tr>
         <td>Original Row</td>
         <td>Data</td>
      </tr>
   </table>
   <br>
   <button id="add-btn">Add Row</button>
   <button id="remove-btn">Remove Row</button>
   <button id="edit-btn">Edit Row</button>
   <script>
      $(document).ready(function(){
         $("#add-btn").click(function(){
            var newRow = "<tr><td>New Row</td><td>Data</td></tr>";
            $("#my-table").append(newRow);
         });
         $("#remove-btn").click(function(){
            $("#my-table tr:last").remove();
         });
         $("#edit-btn").click(function(){
            let rows=$("#my-table").find("tr")
            let idx=Math.floor(Math.random()*rows.length)
            rows.eq(idx).find("td").eq(0).text("Edited Row")
         });
      });
   </script>
</body>
</html>

in conclusion

In this article, we have introduced many methods in jQuery namely they are append(), find() and remove(). We saw how to use these methods provided by jQuery together with some DOM manipulation techniques to dynamically add, edit, and delete table rows.

The above is the detailed content of How to add, edit and delete table rows in jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete