The jQuery Remove function that went haywire

Currently, I am delving into the world of Javascript/jQuery and attempting to construct a list of items in a table based on user input.

The concept is simple - a user types text into a form, the JavaScript/jQuery captures that input and showcases it as a row in a Bootstrap4 table.

https://i.sstatic.net/nXXnm.jpg

https://i.sstatic.net/HDHNe.jpg

I've managed to add the data as a row when a user inputs it (refer to the image), but my goal now is to enable the functionality where, upon clicking the red "remove" button, the entire table-row is removed. At present, clicking the button simply removes the button itself (as depicted in the third image).

https://i.sstatic.net/wCR9T.jpg

Below is the HTML snippet:

<div class="tab-pane fade" id="watchlist" role="tabpanel" aria-labelledby="watchlist-tab">
                  <h2 class="display-4">Watch List</h2>
                  <p class="lead">Add series to your watch list and receive an e-mail notification when new data is available.</p>
                  <p class="card-text">
                    <div class="form-check form-check-inline">
                      <span class="mr-2">How would you like to receive your e-mail notifications?</span>
                      <label class="form-check-label" data-toggle="tooltip" data-placement="top" title="Get email updates on your series bundled in one single daily email.">
                        <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1"> Daily Digest
                      </label>
                    </div>
                    <div class="form-check form-check-inline" data-toggle="tooltip" data-placement="top" title="Get an email instantly when new data is released. You will not get a second email until you've downloaded the most recent data.">
                      <label class="form-check-label">
                        <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2"> Individually
                      </label>
                    </div>
                  </p>
                  <p class="card-text mt-2">
                    <form class="add-items">
                        <div class="form-row align-items-center">
                          <div class="col-md-6">
                            <input type="text" class="form-control mb-2 mb-sm-0" id="watchlist-item" placeholder="Search series by name or symbol...">
                          </div>
                          <div class="col-auto">
                            <button type="submit" class="btn btn-dark" style="margin-left:-12px">Add to Watch List</button>
                          </div>
                        </div>
                      </form>
                  </p>
                  <p class="card-text">
                    <table class="table table-sm table-hover">
                      <thead>
                        <tr>
                          <th scope="col">Symbol</th>
                          <th scope="col">Name <i class="ml-4 fa fa-sort" aria-hidden="true"></i></th>
                          <th scope="col">New Data? <i class="ml-4 fa fa-sort" aria-hidden="true" data-toggle="tooltip" data-placement="top" title="Sort data by latest release."></i></th>
                          <th scope="col"></th>
                        </tr>
                      </thead>
                      <tbody id="table-items">

                      </tbody>
                    </table>
                  </p>
                </div>
              </div>

This is the jQuery section:

$(document).ready(function () {


  $('.add-items').submit(function(event) {
    event.preventDefault();

    var item = $('#watchlist-item').val();

    if (item) {
      $('#table-items').append("<tr>S<td>" + item + "</td><td>S&P 500 Total Return Index</td><td><button class='btn btn-info'>Get Data</button></td><td><button class='btn btn-danger remove'>Remove</button></td></tr>")
      $('#watchlist-item').val("");
    }
  });

  $(document).on("click", ".remove", function() {
        $(this).parent().remove();
    });

});

Answer №1

The button is situated within a td, therefore its immediate parent is the said td, and not the tr. You can either utilize two .parent() calls to move up one level, or employ .closest("tr")

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

How can we assign priority to the child element for detection by the "mouseover" event in jQuery?

Can you help me figure out how to make the "mouseover" event prioritize detecting the child element over its parent? This is the jQuery code I've been using: <script> $(function() { $("li").mouseover(function(event){ $('#log&a ...

Tips for showing error messages in response to exceptions

My function is supposed to display the response text that comes back from ex.responseText. However, every time I attempt it, it returns as "undefined" even though the text is there. onError: function (ex) { $('<div>' + ex._message + &a ...

Generate div elements corresponding to individual objects

Previously, I inquired about a similar issue but have not come across any solutions. I made updates to my code, which is now functioning well, however, I am facing a new dilemma. Here is the PHP code I am currently working with: class individual { pu ...

Typing into the styled M-UI TextFields feels like a never-ending task when I use onChange to gather input field data in a React project

Having an issue where entering text into textfields is incredibly slow, taking around 2 seconds for each character to appear in the console. I attempted using React.memo and useCallback without success :/ Below is my code snippet: const [userData, setUserD ...

what is the best method to schedule tasks at a specific time in node-schedule?

I am facing an issue with running a node task daily at 8AM using the node-schedule package https://www.npmjs.com/package/node-schedule. Instead of running every day at 8AM, it is currently running every minute. What method should I use to correctly configu ...

Example TypeScript code: Use the following function in Angular 5 to calculate the total by summing up the subtotals. This function multiplies the price by the quantity

I have a table shown in the image. I am looking to create a function that calculates price* quantity = subtotal for each row, and then sum up all the subtotals to get the total amount with Total=Sum(Subtotal). https://i.stack.imgur.com/4JjfL.png This is ...

How can I generate an SQL query using the <options> tag and display the result through echoing

I am in the process of creating a page that will show users the cost of repairing their damaged mobile phone. With a database containing all the pricing information for various phone models and issues, I need to figure out how to display the final price af ...

Ionic Vue Modal displays duplicated content

Trying to implement a modal in an Ionic Vue app, I encountered a strange issue where the content is being displayed twice. This problem persists even when using the example code provided on the Ionic website: The modal content component looks like this: & ...

Bootstrap / Blazor - no action when clicking on an image link

Having issues with adding a simple HTML link around an image in my Bootstrap 4.5 / Blazor app. When I click on the link, nothing happens. How is this possible? I've tried using the <a href"..."><img ...> pattern, as well as NavL ...

React Video Component not re-rendering upon state change

I am facing an issue while developing a video player in React. The page retrieves JSON data from the server and creates simple clickable links to change the video content. I have integrated an HTML5 player as a component, passing props such as controls, sr ...

I am experiencing issues with my button not responding when I click on the top few pixels

I've created a StackBlitz version to illustrate the issue I'm facing. It seems like the problem might be related to my CSS for buttons... Essentially, when you click on the button at the very top few pixels, it doesn't register the click an ...

The blackberry device struggles to load the mobile website

Our mobile website solution is powered by the following technologies: Asp.net mvc 4 (running in .net 4) Jquery Mobile (v1.3.0) Jquery validation Is it possible that any of these technologies are incompatible with Blackberry's native browser? We co ...

Exploring the art of manipulating HTML code using JavaScript

I am looking to implement a specific change using JavaScript: <input id="innn" value="" /> to: <input id="innn" value="SOME_VALUE" /> I attempted the following methods: document.getElementById("innn").setAttribute("value", "189"); and als ...

PHP script integration with WHMCS

I've been attempting to add analytic.php into head.tpl in WHMCS, but I keep encountering an error. Analytic.php location: root/analytic.php Head.tpl location: root/whmcs/template/six/includes/head.tpl I read that WHMCS supports Smarty PHP, so I&apos ...

Unable to iterate through Ajax calls using For Loop when onChange event occurs

I'm struggling with looping the OnChange method using an AJAX call, but for some reason, it's not working as expected. for (let index = 0; index < 300; index++) { $('#txtLini'+[index]).on('change', function() { ...

Getting the WatchPosition update just one time

I have implemented this code to continuously retrieve the first position and I need it to keep doing so. var init = function() { navigator.geolocation.getCurrentPosition(function(position) { new_position = position; }, onEr ...

Off Canvas left navigation menu using Bootstrap

I am working on creating a responsive layout for a webpage using Bootstrap. On larger displays, such as desktop resolutions, I want the webpage to show the header and left navigation as normal. However, when the site is resized to that of tablets or mobile ...

Step-by-step guide on deleting an entire row from a PHP webpage

I have removed a row from the database, but now I need to also remove it from the interface on my PHP page. Any suggestions or assistance would be greatly appreciated. Here is a snippet of mypage.php: <tr> <td><?php echo $row[' ...

Is it necessary to close the navigation drawer after selecting an item?

Currently, I'm working on a project for my University where I am trying to incorporate a feature that will automatically close the navigation drawer whenever any of its items are clicked. However, I am facing some challenges figuring out how to achiev ...

Vertical text alignment alongside an image in multiple lines

I have been struggling with aligning multi-line text next to an image in a responsive design. No matter what I try, when the browser window is resized and the text becomes multi-lined, it always falls below the image positioned to the left of it. Can any ...