Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Dynamic Table</title>
- <style>
- body {
- font-family: Arial, sans-serif;
- margin: 20px;
- }
- table {
- width: 100%;
- border-collapse: collapse;
- margin-bottom: 20px;
- }
- th, td {
- padding: 10px;
- text-align: left;
- border: 1px solid #ddd;
- }
- #addRowButton {
- padding: 10px 15px;
- margin-right: 10px;
- background-color: #4CAF50;
- color: white;
- border: none;
- cursor: pointer;
- }
- #removeButton {
- padding: 10px 15px;
- background-color: #f44336;
- color: white;
- border: none;
- cursor: pointer;
- }
- #removeIdInput {
- padding: 10px;
- margin-right: 10px;
- }
- </style>
- </head>
- <body>
- <h2>Dynamic Table with Add/Remove Features</h2>
- <table id="myTable">
- <thead>
- <tr>
- <th>ID</th>
- <th>Name</th>
- </tr>
- </thead>
- <tbody>
- <!-- Table rows will be added here -->
- </tbody>
- </table>
- <button id="addRowButton">Add Row</button>
- <br><br>
- <input type="text" id="removeIdInput" placeholder="Enter ID to remove">
- <button id="removeButton">Remove Row</button>
- <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
- <script>
- let rowCount = 1;
- // Function to add a new row to the table
- $('#addRowButton').click(function() {
- const newRow = `<tr>
- <td>${rowCount}</td>
- <td>Name ${rowCount}</td>
- </tr>`;
- $('#myTable tbody').append(newRow);
- rowCount++;
- // Change button text color
- $(this).css('color', 'blue');
- });
- // Function to remove a row by ID
- $('#removeButton').click(function() {
- const idToRemove = $('#removeIdInput').val();
- $('#myTable tbody tr').filter(function() {
- return $(this).find('td:first').text() === idToRemove;
- }).remove();
- $('#removeIdInput').val(''); // Clear the input field
- });
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement