Tips for handling the closure of a Bootstrap popup upon submission in a VUE framework

I am looking for a way to automatically hide a Bootstrap popup once the "Add Location" button is clicked and the field is filled in.

<template>
  <div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">{{popupTitle}}</h5>
        <button type="button" @click="closeModal" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <p><input type="text" v-model="addBinLocation"></p>
        <span v-show="errorTxt" class="error">Field cannot be empty</span>
      </div>
      <div class="modal-footer">
        <button @click="btnAdd" type="button" class="btn btn-primary">Add Location</button>
      </div>
    </div>
  </div>
</div>



</template>

<script>
export default {
    props:['popupTitle'],
    data(){
      return{
        addBinLocation: '',
        errorTxt: false,
      }
    },
    methods:{
      btnAdd(){
        if(this.addBinLocation === ''){
          this.errorTxt = true;
        }
        else{
          this.$emit('addLocation', this.addBinLocation);
          this.addBinLocation = ''
          this.errorTxt = false;
          this.closeModal();
        }
    },
    closeModal(){
      this.addBinLocation = ''
      this.errorTxt = false;
    }
  }
}
</script>

<style scoped>
  .error{
    color:red
  }
</style>

Within the 'btnAdd' function's else section, I aim to have the modal automatically close when the user has entered information into the field and clicks the button. I attempted using refs with the following code snippet but it did not work:

this.$refs.myModal.hide()

Answer №1

To make your first modal div visible, include a conditional statement v-if="showModal". Toggle the value of this.showModal to true when you want the modal to be displayed and set it to false when you want to hide it using this.showModal = false

Answer №2

When implementing the addBtn function, I successfully managed to conceal the popup by including the following line of code:

 $('#staticBackdrop').modal('hide');

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

Activate event in jQuery when clicking on a blank area, away from any div element

I need help figuring out how to hide a div when empty space on the margins of the page is clicked, as opposed to just any area outside of the div. I've managed to achieve this functionality when certain other divs are clicked, but I'm stuck on im ...

AngularJS - UI Bootstrap: Easily expand or collapse all items in the Accordion widget

I have created a code to open and close all tabs of an accordion individually using separate 'open' and 'close' buttons. However, it requires me to dynamically add a key value pair (a Boolean value) to my JSON data. What is the best ap ...

Develop interactive applications with React Native by generating N animated values

Currently, I am in the process of developing a component known as a "Color Palette," which includes a prop called "paletteColors." The "paletteColors" prop is an array with varying lengths that houses color values represented as strings. Within this comp ...

The error message "Sidenav is not defined" is displayed when Angular 4 / Angular Material 2 cannot find the specified component in the declaration

I recently developed a new project using Angular 4 and Material design 2. Initially, I included a sidenav and a toolbar directly in the app.component.html file, and everything was functioning correctly. However, when I decided to refactor the code and mov ...

Is there a way to transfer the data retrieved from my API to the title in my head using vue-head?

I have been utilizing vue-head on my website to dynamically pass the name of the program to the HTML head. The information is fetched from an API, so I make a request to retrieve it. However, every time I attempt to pass the name, I encounter an error. Her ...

Transforming a series of numbers separated by commas into numerical values

I need to process a list of comma-separated numbers using the math.js library in Node.js. Even though I successfully split the numbers into an array, the math.add() function requires individual integers instead of an array. I want to find a way to provid ...

Instructions for utilizing a non-string reference without triggering flow errors

I've noticed that string refs are considered legacy in React. You can read more about it here: https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-string-refs.md However, I am encountering a flow error in my component and I&apo ...

Retrieving the output from a nested scope within a function

I have a scenario where I am working with a function that has a nested "then" function containing a return statement. My goal is to combine this inner return data with the outer return data. Below is the code snippet: public async getAllWidgets(): Promis ...

Having trouble with the lodash find function in my Angular application

npm install lodash npm install @types/lodash ng serve import { find, upperCase } from 'lodash'; console.log(upperCase('test')); // 'TEST' console.log(find(items, ['id', id])) // TypeError: "Object(...)(...) is un ...

Angular: Cleaning an image tag in sanitized HTML text

I have a scenario where I am integrating an HTML snippet from a trusted external source into my Angular component. To ensure security, I am utilizing Angular's DomSanitizer and specifically the bypassSecurityTrustHtml method to process the snippet bef ...

When large spheres meet, Three.js/WebGL displays them as fractured

I must admit that I am not very experienced with 3D graphics. Issue In my WebGL model using Three.js, I have intentionally made two spheres collide. However, when the spheres are extremely large, they appear "broken" at the point of intersection, whereas ...

The significance of README.md file in every folder within the standard project structure of Nuxt

Why is there a README.md file in each folder of the default project structure? Do we need to leave it there? ...

Tips for fixing Unexpected Token Error in a nextJS project

I recently started working on a brand new "next.js" project and was eager to dive into coding. However, I found myself stuck for hours trying to resolve the following error message. To kick things off, I decided to use a reference code snippet which looke ...

I am looking to modify the background color of characters in a text box once the characters in a textarea exceed 150 characters

Currently, I am utilizing event.data to capture the text inputted into this particular HTML textbox. My intention is to change the background color to red based on that input. However, when using the style attribute on event.data, I encounter an error. It& ...

Vue method not being triggered despite emit in inline template

Trying to figure out what I'm missing here, it seems like this should be the solution: Vue: method is not a function within inline-template component tag Yet, the method still isn't working as expected. <b-table :items="filtered" ...

I'm experiencing difficulty getting my code to function properly when adding or removing classes with Bootstrap Glyphicons

Recently, I decided to play around with bootstrap's glyphicons. By utilizing Jquery's addClass and removeClass functions, I tried to smoothly transition from one glyphicon to another. Here is the code snippet: var glyphOn = true; //The glyphO ...

What is the best way to deploy a REST API utilizing an imported array of JavaScript objects?

I have been using the instructions from this helpful article to deploy a Node REST API on AWS, but I've encountered a problem. In my serverless.yml file, I have set up the configuration for the AWS REST API and DynamoDB table. plugins: - serverless ...

Customize the appearance of a React component depending on its unique identifier

After creating a simple TODO app with drag and drop functionality, I am now looking to apply a line-through CSS style to any task added or dragged into the second column of my app. What is the best way to target these tasks using their unique ID: <div c ...

Using a Component's Variable in Another Component in React Native

Hello there! Thank you so much for offering your help. I have a component called PlayerWidget and would like to utilize its state variable isPlaying value in the AlbumHeader Component. Here is the code for the PlayerWidget: import React, { useContext, use ...

Looking for a solution to my issue - my for loop is looping more times than it should

I am designing a confirm dialog using jQuery and Bootstrap. When the user clicks on 'Yes' or 'No', it should trigger an action, such as drawing two squares each time. The first time I click either button, it draws 2 squares. However, wi ...