"Issues arise when trying to use a Javascript button in conjunction with Bootstrap 4

When creating dynamic divs on a modal form with a close button to destroy them, everything was working fine. However, when I added a button to add new divs/forms items, the close button callback was not being triggered and instead closed the modal without destroying the div itself. The issue seems to be related to the callback function not being called when the rows are created using addNewRow. Below is the code snippet:

console.log("hello");

var myModal = $('#exampleModal'); 

var myForm = $('#exampleModal #myForm'); 
// rest of JavaScript code goes here... 
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <title>Mara test</title>
  </head>
  <body>
    
  <!-- Button trigger modal -->
  // rest of HTML code goes here...
  
  </body>
</html>

Answer №1

After some adjustments, my selector above is now the correct one. Additionally, it seems that your addNewRow() method does not include the Name and Price labels.

console.log("hello");

var myModal = $('#exampleModal'); 

var myForm = $('#exampleModal #myForm'); 
var value = $('#redactorArea3').val(); 
var result = jQuery.parseJSON(value); 

var recordsCounter = 0;

for (var index in result){

  var obj = result[index];
  var mydiv = '<div id="pepe'+index+'" class="input-group mb-3">';
  for (var property in obj){
    var attrName = property;
    var attrValue = obj[property]; 

   
   //myForm.append('<div class="form-group"><label>'+attrName+'</label><textarea class="form-control" id="exampleFormControlTextarea1" rows="1">'+ attrValue +'</textarea></div>');
   
    mydiv += '<div class="input-group-prepend"><span class="input-group-text">'+attrName+'</span></div><input type="text" class="form-control" id="field'+index+'"  value="'+attrValue+'">';

  }  
  mydiv += '<button id="remove' + index + '" class="btn btn-danger remove-me" >-</button></div>';
  myForm.append( mydiv);
  recordsCounter++;
}    
            
function addNewRow(){
    recordsCounter++;
    var mydiv = '<div id="pepe'+recordsCounter+'" class="input-group mb-3">';
    mydiv += '<div class="input-group-prepend"><span class="input-group-text"></span></div><input type="text" class="form-control" id="field'+recordsCounter+'">';
    mydiv += '<div class="input-group-prepend"><span class="input-group-text"></span></div><input type="text" class="form-control" id="field'+recordsCounter+'">';
    mydiv += '<button id="remove' + recordsCounter + '" class="btn btn-danger remove-me" >-</button></div>';
    myForm.append( mydiv);
  console.log("add row");
}

$('#myForm').on('click', '.remove-me', function(e){
debugger;
                console.log("remove me");
                e.preventDefault();
                var fieldNum = this.id.charAt(this.id.length-1);
                var fieldID = "#field" + fieldNum;
                var pepeId = "#pepe" + fieldNum;
                $(this).remove();
                $(fieldID).remove();
                $(pepeId).remove();
                recordsCounter--;
            });
<!doctype html>
<html lang="en>

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

Attempting to extract an API using JavaScript

I'm currently facing an issue with the OpenWeatherMap API where a particular field is returning undefined. Strangely, all other fields are populating correctly except for the weather condition which is accessed at weather.description. Does anyone have ...

Storing Redux state in local storage for persistence within a React application

Recently, I've been experimenting with persisting data to local storage using Redux. My approach involved creating an array of alphabet letters and setting up an event listener to log a random letter each time it's clicked. However, despite succe ...

What methods can be used to disable a JavaScript function, such as utilizing hasClass()?

I am currently customizing a WordPress theme to load posts and pages using AJAX. I have successfully implemented this functionality with the code snippet below, but now I need to prevent the AJAX function from running when clicking on the logo that links t ...

Custom providers do not override Angular UrlResolver

In my Angular application, I am trying to implement a custom UrlResolver provider to incorporate cache breaking logic. I came across this question on Stack Overflow: . Unfortunately, it seems that overriding the default compiler UrlResolver using a provid ...

Guide on placing the back arrow button in React Navigation

I recently started working with react-native and I have been experimenting with react-navigation. One issue that I am facing is the positioning of the back-arrow in the navigation tab. I want to target the back-arrow so that I can adjust its position. Her ...

What is the proper way to select this checkbox using Capybara in Ruby?

Is there a way to successfully check this checkbox?view image description I attempted the following: within('div[id="modalPersistEtapa"]') do element = @driver.find_element(:xpath, '//*[@id="2018_4"]/ ...

Is there a way for me to determine when the modal animation has completed?

I'm currently using the modal feature in twitter-bootstrap and I am curious about how long it takes for the modal to appear before triggering an alert. To better illustrate my point, you can check out this example: HTML <button id="mod" class="b ...

Inconsistencies with AJAX performance

Hey there, I'm a newbie and absolutely loving this site! Currently diving into the world of JavaScript and have been through numerous tutorials. However, I seem to be struggling with getting Ajax to work. Recently stumbled upon this code snippet on ...

Tips for eliminating the backslash introduced by JQuery

Switching back from framework 4.5 to 4.0 caused several issues that needed fixing. One significant change I noticed was that jQuery started escaping double quotes. Is there a way to stop this behavior? I attempted datatest = datatest.replace("\\ ...

Launch the Image-Infused Modal

I am completely new to the world of Ionic development. Currently, I am working on a simple Ionic application that comprises a list of users with their respective usernames and images stored in an array. Typescript: users = [ { "name": "First ...

If the status of any ticket with is_approved=1 is updated to "2", the field ticket_status will be updated based on the order ID in CodeIgniter

This is the table for requests https://i.sstatic.net/NWT4y.png How can I update the ticket_status field with a value of "2" if at least one of the requests has is_approved=1, based on the orderid? I have attempted the following code, but it is not updat ...

Submitting a form in jQuery with AJAX

I am attempting to submit my form using ajax. Below you can find the code snippet I have been working on: $("#loginForm").submit(function() { var url = <url>; $.ajax({ type: "POST", url: url, data: $("#loginForm").serialize(), ...

Show the component on all routes with the exception of a few in react-router-dom version 6

In my web app, I am working on setting up various routes using react-router. Some pages require shared components like the Navigation or Footer, while others do not. What I need is a way to determine if a path does not match specific locations and only re ...

Graphic selectors: a new take on radio buttons

I've been attempting to make this work, but it's not functioning correctly. Below is the CSS code: .input_hidden { position: absolute; left: -9999px; } .selected { background-color: #000000; } #carte label { display: inline-bl ...

Vue.js error: Trying to access an undefined property

Here is my Vue instance setup: const vueApp = new Vue({ el: '#vue-wrapper', data: { items: [] }}); Utilizing a v-for loop in index.html.eex: <div v-for="item in items[0].subItems">{{ item.name }}</div> In this scri ...

Value becomes null when updating in Node.js

I've been working on updating a value through API calls in express and node js. Here is how my route is set up: var express = require('express'); var router = express.Router(); router.put('/updateValue/:id', function(req, res, ne ...

How can I vertically align text in React-bootstrap Navbar Nav-items?

I have been struggling to find a solution to my problem without resorting to custom CSS classes. I'm wondering if it's possible to achieve what I need using only flex classes. Here is the code for a simple navbar component: <Navbar bg ...

Using ThreeJS to load and display several meshes from a .json 3D file

I downloaded a .json file from an online 3D editor and now I'm facing an issue while trying to load and create 20 instances of it, similar to this example. Sadly, my code seems to be flawed as all 20 instances are behaving as if they are the same obje ...

Are there any JavaScript alternatives to Python's pass statement that simply performs no action?

Can anyone help me find a JavaScript alternative to the Python: pass statement that does not implement the function of the ... notation? Is there a similar feature in JavaScript? ...

ReactJS encountered an error: [function] is not defined, July 2017

Attempting to convert a JSON file into an array and then randomly selecting 5 items from it. I suspect the issue lies in my render/return statement at the end of ImageContainer.js, but as a newbie in ReactJS, it could be anything. Any assistance or guida ...