Removing an Element in a List Using Jquery

In my JQuery, there is a list named additionalInfo which gets populated using the function below:

$('#append').on('click', function () {
//validate the area first before proceeding to add information
    var text = $('#new-email').val();
    var li = '<li>' + text +  'input type="hidden" name="additionalInfo" value="'+text+'"/> </li>';
    $('#additional-info-list').append(li);
    $('#new-email').val('');
});

This function not only adds the information to the list for later use, but also creates an <li> element displaying the information. I have implemented a button on each <li> that, when clicked, removes the corresponding list item. However, I am struggling with removing the info text from the additionalInfo list completely. Below is what I have for this method:

$('#removeEmail').on('click', 'li>.remove-btn', function (event){

$(event.currentTarget).closest('li').remove();

});

Can you suggest how I can extract the specific segment of info text from the <li> and then remove it from additionalInfo?

Answer №1

You seem to have encountered a few issues. Firstly, there is an error in your markup when creating new items as the opening bracket of the input tag was missing. I've made changes to the code for deletion so that it now detects the click event on any item with the class remove-btn within the li element. This modification should allow you to delete an item by clicking the remove link inside the li.

$(function(){

  $('#append').on('click', function () {

    var text = $('#new-email').val();
    var li = '<li>' + text +  '<input type="hidden" name="additionalInfo" 
                      value="'+text+'"/>
               <a href="#" class="remove-btn" >remove</a></li>';
    $('#additional-info-list').append(li);
    $('#new-email').val('');

  });

  $(document).on('click', 'li>.remove-btn', function (event){
     var _this =$(this);
     _this.closest('li').remove(); 
  });

});

If you'd like to see the updated code in action, check out this jsfiddle.

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

Effortless event alert specifications for FullCalendar integration with Google Calendar

I've been trying to solve this issue for the past two weeks on and off, and I would really appreciate any help at this point. On a webpage, I have set up a mini calendar using the FullCalendar library. Additionally, I have linked a Google Calendar to ...

The function for converting a jQuery element to a DOM element is yielding a blank string as

What is the reason behind this code not working as expected: function getElementWidth(el){ return $(el)[0].style.width }; getElementWidth('#someElementIdId'); // returns -> "" However, when using this code... function getElementWidth(el){ ...

Failure of AJAX HTML function to retrieve value from textarea

I am displaying data in the first three columns of a table, with the last column reserved for user feedback/comments. However, when the form is submitted, the values in the textarea are not being posted. The table has 6 rows. The Sample TR: <tr> &l ...

Encountering an error message stating "Unable to read property 'map' of undefined while attempting to create drag and drop cards

I have incorporated the library available at: https://github.com/clauderic/react-sortable-hoc The desired effect that I am aiming for can be seen in this example: https://i.stack.imgur.com/WGQfT.jpg You can view a demo of my implementation here: https:// ...

Cease the audio from playing unless you are actively hovering over the image

Is there a way to make the sound stop playing when you are not actively hovering over the picture? Any suggestions on how to achieve this would be greatly appreciated! imgarr[i].onmouseout=function(){ this.setAttribute('src',this.getAttribu ...

Accessing JavaScript results in PHP

Hello everyone! I am working on creating a dropdown menu for selecting time using JavaScript to get the computer's current time. My goal is to have an output that automatically selects the option in the dropdown if it matches the computer's time. ...

Enhance your CSS link in Yii framework 2.0 by incorporating media printing capabilities

While working on Yii framework 2.0, I typically include a CSS file by adding the following code snippet to assets/AppAsset.php: public $css = [ 'css/style.css', ]; Upon inspecting the element in the web browser, I notice the following code ...

Angular 11 along with RxJS does not support the combineLatest method in the specified type

Hey there, I'm currently working on utilizing the combineLatest operator to merge two streams in Angular, but I keep encountering an error message stating that "combineLatest does not exist on type". I've attempted to move the code into a .pipe() ...

translateZ function fails to function properly on Firefox browser

I've been experimenting with using translateZ to hide a child element called "list" behind the parent element "div2. It works perfectly in Chrome, but unfortunately, it's not working correctly on Firefox. Can anyone provide some guidance or help ...

Add a border to the element if its height exceeds 300 pixels

My web page has a dynamic element with an unpredictable height. As more content is added, the element grows, but I have restricted it with a max-height: 300px;. However, I would like to add a hint to the user when the element reaches its maximum height by ...

Check if the asp.net textbox meets the criteria of being exactly 19 characters long and is in the correct format using J

I have a web application developed in asp.net, which incorporates both bootstrap and jQuery. I am seeking a way to validate text input based on specific criteria: the text must be exactly 19 characters long and follow this format: - The first three charact ...

JavaScript - Functions in objects losing reference to previously created object properties

Having trouble with my Candy function. When I create an object of the Candy function, all attributes are created correctly. However, when I try to run the draw function, it always uses the properties of the second object created instead of the one I want. ...

Best practice for updating Form.Control text using a custom onChange method

Today, I observed a unique behavior while utilizing a custom onChange in a Form.Control. The text in the field no longer updates when a file is selected. I thoroughly checked the Git documentation HERE, but unfortunately, it does not provide information on ...

Enable the Angular button only when the radio button has been selected

Just starting out with Angular and I have a query. This is a scenario for my project at work. https://i.stack.imgur.com/R3SxA.png In this screenshot, I want to enable the "ajouter" button when at least one radio button is selected in the secure chest (s ...

Having Trouble Loading PHP File with Jquery

I've been struggling with using JQuery/Ajax to load the content of my PHP file into a div tag. Below is the code snippet from my page that attempts to load the file: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/ ...

Retrieve the child element that is being clicked

Alright, I'm facing a little issue here (it seems simple, but I just can't seem to crack it)... Let me paint the picture with a snippet of HTML code below: <!-- New Website #1 --> <!DOCTYPE html> <html style='min-height:0px; ...

"Embracing Dynamism: Enhancing Vue3 with Dynamic Routing for

Seeking guidance on implementing a dynamic component with Dynamic Routing in Vue3. The goal is to render a component based on the parameter (path named id) from router.ts. In the router.ts file, there is a dynamic parameter called id that needs to be used ...

Maintaining selected options in select lists while updating model data in Angular

New to Angular and exploring the Product object with Sku objects nested inside. An app allows users to fetch a product, resulting in the Product object being assigned to $scope.product: var app = angular.module('app', []); app.controller(&apos ...

What could be causing the issue with my validation for alphabetical input?

I am currently working on a registration form that only accepts alphabetical input. However, I am facing an issue where my error message appears regardless of whether I input an alphabetical or special character. According to my understanding, the code sho ...

Looking to attach a listener to an element within a modal once it has finished rendering?

Upon clicking a button, a modal window appears. The controller assigned to the modal contains a list of element IDs that need listeners assigned. However, when the controller initializes, the modal has not yet rendered, causing the elements requiring liste ...