How to Deactivate Button Listener within Bootstrap Popover Content

Is there a way to submit different input data through a shared pre-generated HTML form template that uses unique variable names as titles? I have two buttons on my page with similar popover effects. When I click the submit button inside a .popover-content, both forms get submitted, even though one of them is hidden.

function handlePopover (button) {
  const id = button.attr('id');
  $(document).on('click', '#close', function(){
    botton.popover('hide');
  }
  $(document).on('click', '#submit', function(){
    if(id === 'button1') alert("button1");
    else if(id === 'button2') alert("button2");
  }
}

This is how I implemented the function:

const button1 = $("#button1");
const button2 = $("#button2");
handlePopover(button1);
handlePopover(button2);

I suspect the issue may be due to adding a listener like this: $(document).on(...). However, I am unsure how to resolve this problem. Is there a way to deactivate the listener when the popover content is hidden? Thank you for your assistance!

Answer №1

After some experimentation, I have discovered the solution. The key is to attach a handler to the popover button prior to attaching a handler to the submit button.

button.on('shown.bs.popover', function() {
  // set up a handler for the submit button
  $(document).on('click', '#submit', function(){
    if(id === 'button1') alert("button1");
    else if(id === 'button2') alert("button2");
  }
}

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

What are the steps to generate two unique instances from a single class?

Is there a way to output two instances of the class Cat : Skitty, 9 years and Pixel, 6 years, in the console? (() => { class Cat { constructor(name, age) { this.name = name; this.age = age; } } docume ...

What is causing the background image size to adjust when the carousel is toggled?

Can you help me understand why the background-image increases in size vertically when transitioning from url/path to url/path2? I need it to stay the same size as it toggles through all the images. What mistake am I making and how can I correct it? Snipp ...

What is the best way to determine in component.html whether the column type is equal to 1 to show the label text "Active,"

Having trouble checking the value of an object named ReportControl. If the column type is 1, display the label "active"; otherwise, display the label "not active" on reportcomponent.html. The data for the ReportControl object is as follows: {"reportId": ...

Navigation Menu in Motion

I'm currently working on a website for my F1 in Schools team and I'm looking to implement a feature where the button in the navigation bar changes its background color and font color when that section of the page is active, even while scrolling o ...

Having trouble displaying data on the front end with Node.js and hbs due to issues with Json parsing

Server side: app.get("/basket", (req, res) => { fs.readFile("products.json", (err, data) => { if (err) { res.status(500).end() } else { res.render("basket", {products: JSON.parse(data)}) } ...

Combining strings in a JavaScript object

Can someone help me with concatenating strings within an object in JavaScript? I am parsing through an XML file to create a list of links in HTML. Each time I iterate through the loop, I want to add a new <li> element containing the link. How can I ...

Incorporate a Google Chart link by integrating it with a select handler

I'm facing an issue while trying to open a link in a Google line chart using the selection handler. The chart stops rendering, and as a newcomer to javascript, I'm unsure why. This is how the code is integrated into my HTML: <pre><code ...

Achieving Vertical Stacking and Responsive Scaling for Row and Column Containers on Mobile View Using HTML, CSS, and Bootstrap 4

Utilizing the Bootstrap grid system to organize text boxes in a 2 by 2 layout on a webpage. The desktop view displays the content in rows and columns effectively, but it would be beneficial to have an easy way to adjust the width. However, the problem ari ...

Show the result as "Content-Type: image/jpeg" on the webpage

I have a URL for an API request that automatically downloads and saves a QR code image from the browser. The Content-Type of the URL is application/jpeg, and its format looks like this: application(websiteURL)/egs?cmd=gen_qrcode&customer_id=123&n ...

Ways to cease a setInterval after a specified duration or a specific number of iterations

I have successfully implemented a jQuery code for creating a loop of "changing words" by referring to the solution provided in this Stack Overflow answer: jQuery: Find word and change every few seconds My question is, how can I stop this loop after a cert ...

Comparing the size of an array property in Mongoose with another property's size

Is it feasible to retrieve all documents containing an array property with a size greater or less than a specified number property? We are assuming that the numberProperty within the schema is of type Number and the array holds ObjectIds. For example: Co ...

Authenticate yourself as a user or an organization on mongodb

I am currently developing an application that requires user registration and login, as well as organization registration and login. I have implemented the use of Node.js Passport with a local strategy for authentication. While I have successfully created t ...

Error: Invariant Violation occurred with code 29 while using React Apollo and GraphQL

I encountered an error that says "Invariant Violation: 29." Could someone explain what this error means and if I missed something in my code that triggered it? The error occurred when I was trying to import the LocationSearch component into my index.js. im ...

Using JQuery to delete an item by clicking on the delete button and then clicking on the item to remove it

I am currently using jQuery to dynamically create items on an HTML canvas for users to drag around and create drawings in a style similar to Microsoft Visio. However, I am struggling with how to remove these items once they have been created. While I know ...

What is the most effective method for declaring callbacks on objects in Typescript?

I am currently working on a sidebar menu component that is connected to a service holding items in the menu. This allows multiple sources to make alterations to the menu as needed. Each item in the menu currently follows the SidebarItem interface: export ...

What is the process for creating a new Object based on an interface in Typescript?

I am dealing with an interface that looks like this: interface Response { items: { productId: string; productName: string; price: number; }[] } interface APIResponse { items: { productId: string; produc ...

Utilizing JavaScript or jQuery to adjust the cursor pointer value based on user input

Issue: Need help with live updating of input value to range input pointer [Check out my CodePen for reference][1] Adjust upper range input pointer based on lower range input pointer ][2] I am currently working on a range-to-range calculator for a book ...

Issue with rendering D3 horizon chart is not displaying

I am attempting to create a horizon chart using D3.js and Horizon.js, inspired by this example from Jason Davies. I am working with fitness tracker data. However, the example by Mike Bostock uses a unique JSON structure and performs some complicated row-t ...

Load the file's contents into an array within a Vue component

https://i.sstatic.net/gxcYi.png My media.txt file contains the following URLs: "https://www.dropbox.com/s/******/687.jpg?dl=0", "https://www.dropbox.com/s/******/0688.jpg?dl=0 Incorporating a Vue carousel component: <template> <section> ...

What factors influence Redux in determining when to update the user interface?

As per the design, when the state updates, the UI should also update accordingly. However, if I return a completely new object, it seems to have no effect on the UI. case 'clearArticleForm': let newState1 = {}; return newState1; } E ...