The checkbox bootstrap functionalities become ineffective when applied through jquery append

I'm facing an issue with Bootstrap usage in the html section for checkboxes. It works fine there, but when I try to use it in the JavaScript section to append a tag, it doesn't seem to work.

Currently, I haven't been able to find a solution to this problem. What I want is for the functionality that works in the HTML section to also work the same way when added through JavaScript.

It seems like Bootstrap is not being utilized properly for the checkbox element.

let addItem = (value) => {
  $("#todo").append(`<li>
  <div class="custom-control custom-checkbox mb-3">
  <input type="checkbox" class="custom-control-input" id="customCheck" name="example1">
  <label class="custom-control-label" for="customCheck">${value}</label> 
  <button type="button" class="btn btn-danger" id="delete"><i class="fas fa-trash"></i></button>
  </div>
  </li>`);
}

Answer №1

Bootstrap styling is already in place at the time of HTML rendering and does not reapply itself after executing JavaScript to add a new section.

Answer №2

$('#todo').on('click', '.delete', () => { $(this).parent().remove();});

Your code is utilizing an arrow function as the callback. Arrow functions do not bind the 'this' variable by default. To resolve this issue, consider using a regular function declaration for the callback instead. Here's an example:

$("#todo").on("click", ".delete", function() {
  console.log("this", this);
  $(this)
    .parent()
    .remove();
});

If you prefer to continue using arrow functions for the callback, you can pass the event parameter explicitly like this:

$("#todo").on("click", ".delete", event => {
  console.log("this", event);
  $(event.currentTarget)
    .parent()
    .remove();
});

In addition, since you are using Babel in npm run build, it will convert your ES6 code to ES5 implementation. This ensures compatibility with older browsers that may not support ES6 JavaScript features.

For more information on arrow functions and when to use them, you can refer to the following article:

https://www.freecodecamp.org/news/when-and-why-you-should-use-es6-arrow-functions-and-when-you-shouldnt-3d851d7f0b26/

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

choose a value from a dropdown menu to reset other dropdowns to their default values

I am encountering an issue with my PHP form that consists of 7 dropdown lists. Whenever I select a value from one of the dropdown lists, I want the other 6 to reset to their default values if they were previously opened. I believe using JavaScript is neces ...

Hold off on running addThis

My website utilizes Google Maps along with my own JavaScript functions. Additionally, I am integrating the AddThis JavaScript for social sharing buttons. For optimal performance, I need both my custom JavaScript and Google's JavaScript to load and exe ...

Can markdown generated HTML be bound to an Angular 4 component?

I'm currently using Angular 4 to develop an app similar to a wiki, allowing users to create their own pages and link them using a markdown text editor controlled by a component/directive. That's the goal, at least. For example: The custom markd ...

Accessing the front camera on a laptop using mediaDevices.getUserMedia

I am currently working on an internal web application for our organization, with the main users accessing it through desktop or laptop machines. One of the features I need to implement is the ability for the user to take a selfie. To achieve this, I refer ...

Replacing strings with special characters appears to be malfunctioning

After spending countless hours searching through Stack Overflow and other resources, I am still unable to understand what is happening here. Any assistance would be greatly appreciated! I am trying to convert document.write('</div>'); to - ...

Is your iPhone struggling to display animation sprites correctly?

After testing on Android and iPhone, I found that the website works great on Android but lags on iPhone. I suspected that the issue might be related to image preloading, so I tried adding a simple jQuery preloader, but it didn't solve the problem. Th ...

Connecting users from a mobile website to a particular item within the Amazon application

Recently, I've been working on linking from my mobile site to a particular item within the Amazon app. My JavaScript code includes a try/catch block that redirects users to the webpage if they don't have the app installed. However, I've foun ...

I can't seem to view the Request Payload of 10 million characters in the Chrome Network tab

Operating on a Mac with version 10.13, I have encountered an issue while using Chrome devtools. When inspecting an XHR request and clicking on the Headers for an application/JSON request with a payload exceeding 10 million characters, I am only able to see ...

Is it possible to utilize axios in Vue while utilizing CORS in the API?

I am encountering an issue with making a GET request to a CORS enabled corona virus API using axios and Vue. I have no control over their server, and my Vue app was created with vue-cli. Interestingly, I am able to make two requests from different APIs - ...

Built-in validator in Bootstrap 4 - Error messageText

I used Bootstrap 4 to create a basic form and decided to utilize the built-in validator in BS4. For the email field, I implemented the following code: <label for="email">E-mail cím:</label><br> <input type="email" name="email" class ...

Achieve full implementation of ng-repeat within an angular directive

Can you explain how the custom angular directive 'myscroll' is able to detect and interact with the ng-repeat elements that are dynamically created in the code snippet below? <myscroll> <div ng-repeat="item in items">{{item}}< ...

Modifying the default value setting in MUI Datepicker

Currently, I am utilizing version @mui/x-date-pickers ^6.17.0. Here is the custom date picker I am using: https://i.stack.imgur.com/k8nF1.png Upon clicking the input field, the placeholder switches and a value gets appended. However, modifying the input ...

Using the div tag to create a dropdown menu in an HTML document

I have created a search bar with a filter to provide accurate search results. The filter includes three values: areaUnit, area, and price. However, the current layout is not user-friendly as it disrupts the position of elements on the page. I would like th ...

Utilizing the float-left class in Bootstrap 4 on a div cancels out the link between <a><img></a> elements

Strange scenario... when using this code, a picture appears with the related text BELOW it. It's crucial that the picture remains clickable, but the text isn't wrapping as desired (see below): <div class ...

A guide on updating and monitoring the boolean state of individual elements within an array

I am attempting to create a feature in react native where pressing a TouchableHighlight changes its color. Currently, I have a state variable that toggles between true and false when the button is pressed. However, this causes all elements in the map to ...

Using React to Calculate the Total Value of Child Components within the Parent Component

In this scenario, the parent component has access to the values of the child components but struggles to calculate the sum correctly. Instead of displaying the accurate total, it shows 0. https://i.sstatic.net/1etAx.png The expected behavior is for the To ...

Clicking on the following link will initiate the process of submitting a form

I'm encountering an issue with the following code snippet: <a href='javascript:jQuery("#questionnaire").show();'>haha</a> When this code is placed inside a form, it mysteriously causes the form to submit upon clicking the link. ...

Employ a directive within a different directive

I have developed a directive for displaying SVG icons and now I want to use this icon directive within another directive. Icon directive: <icon p="shopping-add"></icon> What I am aiming for: app.directive("product", function() { return { ...

What is the best way to open a new window, such as a music player, and integrate it into a Shopify environment

Currently, within my Shopify store, I am using the following code: <script language="javascript" type="text/javascript"> function popupwindow(url, winName, w, h) { var left = (screen.width/2)-(w/2); var top = (screen.height/2)-(h/2); return wi ...

Unusual behavior of the splice function when applied to child nodes

Inside the given ul element, there are four li child elements. If the code below is run in the console: var a = document.getElementsByTagName("ul")[0]; a.childNodes.splice(1, 2, "abc", "cde"); An error will appear with this message: TypeError: a.childNo ...