Issue with Jquery/Js function not performing as expected

This Javascript function is really effective in adding and removing form elements. However, I have noticed that it does not remove the class "input-group col-md-12 row" when an element is removed.

I would like to modify it so that it also removes that class when an input field is deleted.

Additionally, I want to eliminate the use of the "p" tag for a cleaner structure.

You can view the example on Jsfiddle (missing some CSS, you can only see the leftover class with Firebug): http://jsfiddle.net/tZPg4/8270/

The Function:

 $('#addScnt').on('click', function() {
          $('<div style="width:104%; margin-left:-4px; margin-bottom:5px;"  class="input-group col-md-12 row" style="padding:0;"><p> <input  class="col-md-12"  id="form-field-1" type="text" placeholder="Milestone"> <a href="#" class="remScnt">Remove</a></p></div>').appendTo(scntDiv);
          i++;
          return false;
        });

        $(document).on('click','.remScnt', function() { 
         if( i >= 2 ) {
          $(this).parents('p').remove();
          i--;
        }
        return false;
      });
      });

Answer №1

To completely remove the entire section, you can use the following code:

$(this).closest('.input-group.row').remove();

This is preferable over:

$(this).parents('p').remove();

The .closest() function looks for the nearest ancestor (including the element itself) that matches the specified selector by traversing up the DOM hierarchy. In this case, it starts from the <a> tag.

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

Issues with AngularJS's ng-options functionality are hindering its performance

Having trouble with ng-options not working as expected? Despite following the required structure, it seems like you're getting the same result. Don't worry, I've been researching the topic endlessly and could use some help from you guys. HT ...

Communication between nodes using serial ports in Node.js fails to receive a response from the connected Arduino board

I've been attempting to establish communication between a computer and an Arduino board using node.js. Despite having a simple program, it just doesn't seem to work as expected. The Arduino program (which seems to be working fine) is as follows: ...

extracting generated hyperlinks through a JavaScript function via webdriver

Looking for help with accessing a tricky href that cannot be hardcoded due to dynamic generation: Base classes https://gist.github.com/codyc4321/724f05aca8f6775e2fc1 (access_link of interest) class HCCDriver(FirefoxDriver): def __init__(self, userna ...

Setting the height and width to 100% causes the element to slightly protrude

After setting the height and width of a canvas to 100%, I noticed that instead of fitting the screen perfectly, it slightly exceeded the boundaries, causing a scroll bar to appear allowing me to scroll just a few pixels up, down, left, or right, even after ...

What is the best way to strip the text from a link that encompasses both text and an image using jquery?

I have a website with a list of items. Each item in the list has a link that includes both text and an image. Here is an example of the HTML structure: <li class="li-one" style=""> <a href="#" onclick="return ...

Trouble with CSS table background display in Outlook

I am experiencing issues with an HTML table in an email template: <table id="x_formHeaderTable" style="width:100%; margin:0; padding:0; background-color:#FCE68D; border-style: solid; border-color: #000000;"> <tbody> <tr> &l ...

Is there a way to determine if any word within a given text is present in an array?

Imagine you have the following full text: var nation = "Piazza delle Medaglie d'Oro 40121 Bologna Italy" And a given array like: ["Afghanistan", "Italy", "Albania", "United Arab Emirates"] How can we verify if the exact word Italy within that ent ...

Using Bootstrap 4: How to maximize the width of a span element placed near a label

I need to adjust the width of a span next to its label in my project. My goal is to show multiple groups {label / data} on the same line. To achieve this, I divided the row into 4 columns and tried to set a specific width for a span. However, no matter wh ...

Using Vue's md-input with Vuex results in data updating in a single direction

My Vue application has an input element connected to a Vuex store like this: <template> <input v-model="this.$store.state.myvalue"/> </template> The code in my VueX store/index.js is as follows: import Vue from "vue"; import Vuex fr ...

Securing PHP Sessions with Unique Keys

Having some trouble with implementing PHP login and user validation once they access the system. While I have a basic understanding, I'm uncertain if my approach is correct. Let me walk you through it step by step. Firstly, there's a form for i ...

How can I send a current variable through PHP?

I have a pressing deadline and I need some guidance before moving forward. Can someone please advise if what I'm attempting is feasible or if there's a quicker solution? I encountered some problems with an accordion menu, so as a temporary fix, ...

Transfer properties to the children of this component

I'm a beginner with React and facing an issue when trying to pass custom props to this.props.children. I attempted using React.cloneElement, and while I can see the prop in the console.log within the class where I created it, it seems to get lost duri ...

Looking to incorporate nested rules in `JSS` using `CSS`?

Can someone guide me on how to use CSS class-nesting in Material-UI or JSS in general? I have been attempting the following: card: { cardHeader:{ marginTop:"30px" } } ...

Shut down two modal windows and then open one anew

I am facing an issue with fancybox where I need to open 2 modals of a plugin and then close 2 and open 1 again... For example: closing 2 modals and opening 1 modal again... http://jsfiddle.net/g3R75/1/ I have tried the following code but it doesn't ...

Utilizing intricate nested loops in Angular.JS for maximum efficiency and functionality

Struggling to work with data looping in Angular.JS, especially when it comes to specific formatting Let's illustrate what I'm aiming for using Java Here's a snippet: int itemCount = 0; for(int i = 0; i < JSON.length(); i = i + 3) { ...

Refresh the context whenever the state object changes

Within my application, I am utilizing a PageContext to maintain the state of various User objects stored as an array. Each User object includes a ScheduledPost object that undergoes changes when a user adds a new post. My challenge lies in figuring out how ...

Having trouble getting typeahead suggestions to display in Bootstrap UI when using AngularJS?

I am having trouble getting static suggestions to display in a search textbox. I have tried following the example at this link. Below is the code I am working with: Here is my HTML code: <div> <input type="text" name="questions" id="que ...

React-Native - Issue with TypeError: attempting to call a function that is undefined (specifically when using the 'object.map' method)

I've tested everything from the itemList to the reducer and action, it's working fine and successfully deleting the desired item. However, I encountered an error afterwards. I'm not sure where I went wrong. Can someone guide me on what steps ...

The custom icon font sourced from Flaticon and hosted on a separate server is not functioning properly

I recently obtained a unique icon font from Flaticon and decided to incorporate it into a SaaS online store. Since I don't have direct access to the shop's files, I attempted to host the font on an FTP server and integrate it on a demo page: here ...

Updating image attributes using jQuery within an .each loop

My challenge involves a textarea where I paste HTML code, followed by a button that should display all the images from that code along with their respective alt text and titles. The goal is to easily update the alt text and titles for each image individual ...