I'm attempting to conceal a div five seconds after the page loads, but for some reason, it's not working. What could be the

Here is the snippet of code I'm working on:

<div class="pop-up-chat">
    <div id="div1">
        <div class="pop-up-msg regular-text">Hi, how can I assist you?</div>
        <div class="triangle-down"></div>
    </div>
</div>

Currently, I am trying to target the pop-up-chat div and add a 5-second delay before hiding it. However, it seems to be encountering some issues. Can you spot the problem?

export default {
    name: 'PopUpMsg',
    mounted() {
        this.msgShow();
    },
    methods: {
        msgShow() {
            const msg = document.getElementsByClassName('pop-up-chat');

            setTimeout(() => {
                msg.style.visibility = 'hidden';
            }, 5000);
        },
    },
}

Answer №1

To display a pop-up message, simply add a new property called showPopup to the data object and initialize it as true. Then, within the mounted hook, change it to false.

You can use the v-show directive to control the visibility of the element you want to show or hide. For more information on using v-if-vs-v-show.

<template>
  <div>
    <!-- Binding style -->
    <div :style="{visibility: showPopup ? 'visible' : 'hidden' }"

    <!-- Or use v-show directive to show | hide element -->
    <div v-show="showPopup" class="pop-up-chat">
      <div id="div1">
        <div class="pop-up-msg regular-text">Hi, How may I help you?</div>
        <div class="triangle-down"></div>
      </div>
    </div>
  <!-- The rest of your template -->
  </div>
</template>

<script>
export default {
  name: 'PopUpMsg',
  data() {
    return {
      showPopup: true,
    }
  }
  mounted() {
    this.msgShow();
  },
  methods: {
    msgShow() {
      setTimeout(() => {
        this.showPopup = false
      }, 5000)
    },
  },
}
</script>

Alternatively, you can create a custom directive called v-visible for toggling visibility.

<div v-visible="showPopup" />

Vue.directive('visible', function(el, binding) {
    el.style.visibility = Boolean(binding.value) ? 'visible' : 'hidden';
});

Answer №2

If you're using Vue.js, my recommendation is to utilize the v-show directive for displaying and hiding the popup.

Check out this demo:

new Vue({
  el: '#app',
  data: {
      isVisible: true
  },
  mounted() {
    this.poppupShow();
  },
  methods: {
    poppupShow() {
      setTimeout(() => {
        this.isVisible = false;
      }, 5000);
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div class="pop-up-chat" v-show="isVisible">
    <div id="div1">
      <div class="pop-up-msg regular-text">Hi, How may I help you?</div>
      <div class="triangle-down"></div>
    </div>
  </div>
</div>

Answer №3

This is how it operates

methods: {
    displayMessage() {
        const message = document.getElementById('message');
        setTimeout(() => {
            message.style.display = 'block';
            setTimeout(() => {
                message.style.display = 'none';
            }, 10000);
        }, 5000);
    },
},

.chat-box { display : none; //initially hidden }

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

npm WARNING: The package @angular-devkit/[email protected] is in need of a peer dependency xxxx, however no installation for this dependency has

Attempting to launch an angular project and encountering the following errors: $ npm install npm WARN @angular-devkit/[email protected] requires a peer of @angular/compiler-cli@^14.0.0 but none is installed. You must install peer dependencies yoursel ...

Steps for eliminating an element upon the second click:

I am faced with a challenge where I need to dynamically add elements to a container on the first click and delete them on the second click. I feel like I am complicating things unnecessarily and there must be a simpler and more elegant solution available. ...

Engage with React application using an Android WebView

My setup is simplified below: //--- example.com application ---// async fetchFromServer() { // returns a promise.. } function App() { const [exampleState, setExampleState] = React.useState(null) React.useEffect(() => { fetchFromSe ...

iOS alert notification for navigator

How can I fix the issue with my alerts not working on my iOS project? I am using Ionic and AngularJS to develop my app. The problem I am facing is that when the alert pops up, the title shows as "index.html". This is how I call the alert: alert("aaa"); ...

ways to navigate to a particular vue js component

Is there a way to perform a URL redirect without refreshing the page, without relying on a router link like the example below? <router-link to="/about us" active-class="active">foo</router-link> Instead, I would like to display routes in the ...

Contrasting images showcasing Headless vs Non Headless settings in Puppeteer

I'm currently attempting to capture a screenshot or PDF of the content available at this URL. When using the option {headless: false}, the screenshot is generated correctly; however, in headless mode, some images do not render in the screenshot (for e ...

Encountering a problem where PDF is displaying squares instead of text while using ReactDOM.createPortal to

Currently, I'm working on a React application where I am using createPortal in ReactDOM to open certain components in new windows. Everything seems to be functioning correctly, except when I try to render PDF files. Strangely, everything works fine wh ...

I'm having trouble understanding why my Javascript validation suddenly stopped functioning. Can anyone assist me in troubleshooting this issue?

I have been working on this webpage for a school project for a few days, and it was running smoothly until about 10 minutes ago. The only change I made was adding an extra JavaScript validation. Now, when I try to register by clicking the "register" butt ...

Creating and fetching CSV files using PHP and AJAX

I have successfully created a PHP script that generates a CSV file for automatic download by the browser. The code below shows a working version with sample data for different car models. <?php $cars = array( array("Volvo",22,18), array("BMW",15,1 ...

Other options for the keyup event handler

Currently, I am working on a popup feature where a login popup appears when the user clicks on it. My goal is to verify the username and password against the database before allowing the login button to be activated. I have implemented an Ajax function wit ...

Pass a JavaScript array variable to a PHP script utilizing jQuery Ajax and receive a string output instead of an array

Whenever I attempt to transmit a JavaScript array to a PHP script via Ajax, the output of $_POST['file_paths'] when var_dumped shows up as a string instead of an array. My goal is to have the output in array format rather than in string format. C ...

Is it possible to retrieve only the attributes in an array?

https://i.sstatic.net/E1DMb.png I am working with an array that has properties embedded within it. I am wondering if there is a method to separate out these properties from the array data and transform them into a distinct object containing only the prope ...

What is the best way to modify the state of a nested component?

I am facing a challenge in my React project where I have two buttons in my App.js. When either of the buttons is clicked, I want to change the current state (list) to display in descending order based on the button pressed (order by date or order by upvo ...

Attempting to create a digital keyboard using Bootstrap with the help of JavaScript

In this interactive feature, there is a simple textbox with a virtual keypad that appears and disappears when the user clicks on the keyboard icon inside the text box: https://i.sstatic.net/fdTkn.png Each letter on the virtual keypad is clickable, allowi ...

How come process.env.username is pulling the computer hostname (in Windows 10), rather than the key-value pair specified in the config.env file?

In my config.env file, I set up username and password variables and attempted to retrieve them using the dotenv package. However, when I try to access the username variable, it returns the hostname instead. This happens regardless of whether I use lowerca ...

Tips for sending values via props to a different component and the common error message: "Consider using the defaultValue or value props instead of setting selected on the <option> element"

Currently, I am attempting to pass the selected value using the prop: handle state change, but encountering two errors. Error 1 : Instead of setting selected on <option>, use the defaultValue or value props. Error 2 : A property 'active' ...

Error: ProductManager cannot be used as a constructor

Within my JavaScript file, I have defined a constructor and some methods (add, get, etc). However, when I attempt to call it from another JS file, I am encountering the error message: TypeError: ProductManager is not a constructor const fs = require(' ...

The Content Security Policy is blocking other sources because it is set to 'self

Having these two inline script tags: <script async defer src="https://apis.google.com/js/api.js"></script> <script async defer src="https://accounts.google.com/gsi/client"></script> important to note: attempted ...

Is there a way for me to direct to a different HTML page once I click on a certain data point?

At the moment, I have been utilizing this complete set of calendar codes. <!DOCTYPE html> <html> <head> <meta charset='utf-8' /> <link href='fullcalendar.css' rel='stylesheet' /> <link href=&a ...

Tips for assigning an event to a variable in JavaScript

Here is my javascript code snippet: function drag(ev){ var x= ev.dataTransfer.setData("Text",ev.target.id); } I am trying to pass a variable within this event so that I can perform a specific action. However, the current implementation is not worki ...