Is it feasible to display a message for a certain duration without using the alert() function upon clicking a button?

I'm working on a NEXT.JS project and I need to display a <p> element under my button for a specific duration before it disappears. I don't want to use the alert() function. Any suggestions on how to achieve this?

Answer №1

To accomplish this task, you can utilize a basic JavaScript event trigger. Simply set up a click event listener for the button and then associate your 'p' element with the desired display area.

By the way, this is unrelated to next.js as it involves fundamental JavaScript DOM manipulation. It is important to understand how the DOM works before delving into frameworks like next.js. A solid grasp of core JavaScript and DOM manipulation is essential for mastering frameworks effectively.

Answer №2

If anyone is looking for it, here is a solution that utilizes useState along with a simple handler...

const [showAlert, setShowAlert] = useState(false)
  
  const handleShowAlert = () => {
    setShowAlert(true);
    setTimeout(() => {
      setShowAlert(false)
    }, 5000);
  } 

You can implement it like this:

<button onClick={handleShowAlert}>Click me</button>

and for <p> use

style={showAlert ? {display: "block"} : {display: "none}}

Answer №3

Although I may be late to the party, I'm still going to post this and give the snippet method a shot.

function displayMessage(e){
            let interval = showMessage(e);
        }
        
        function showMessage(e){
            document.getElementById("alertPrompt").innerHTML="Click-me!";
            document.getElementById("alertPrompt").style.display="inline";
            interval = setInterval("hideAlert()",3000);
        }
        function hideAlert(){
            document.getElementById("alertPrompt").style.display="none";
            clearInterval(interval);
        }
html, body{
        font-family:"sans-serif";
            font-size:1em;
        }
        button{
            font-size:1.2em;
        }
        #buttonContainer{
            width:200px;
            margin:auto;
        }
        #alertPrompt{
            display:none;
            text-align: center;
            border:1px solid #000000;
            background-color:red;
            color:#FFFFFF;
        }
<div id="buttonContainer">
        <button id="clickableArea" onMouseOver="displayMessage()">Alert Button</button>
        <p id="alertPrompt"></p>
    </div>

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

"Unlocking the Power of Colormap in JavaScript: A Comprehensive

I am in need of incorporating a colormap into my JavaScript page. My server side is powered by Flask, written in Python. To render colormaps on the client side, I have a JavaScript file that requires the colormap module (installed using "npm install colorm ...

Validating phone numbers with regular expressions in Java

I recently started learning Java and Regex, and I'm currently facing a challenge with phone number validations. The task at hand is to create simple phone number validations. I already have an input field in my HTML code that has the attribute type=" ...

Unable to identify the Xpath selector for the "Account settings button" and "find my iPhone" features within iCloud

When trying to access my iCloud account settings and locate the "Find my iPhone" button on iCloud.com, I am encountering issues with my selectors. It seems like there are no frames on the page, but the Xpaths I'm using are not matching. Here are the ...

Dealing with multiple POST requests at once in Node Express JS - Best Practices

I've been working on a Node project with Express to handle incoming GET/POST requests. I have set up routes to manage various types of requests. One specific route, /api/twitter/search, calls a function that uses promises to retrieve Twitter feeds and ...

What effect does the addition of a border-top have on the position of the left column, causing it

Currently, I am working on creating a list with each list item having a bottom border of 1px solid. The list is then divided into 3 columns using column-count:3 CSS property. For instance, if there are 8 items, it should display 3 items in the first column ...

Creating a design with CSS that features three main content columns of equal height, all of which are scrollable and have fluid heights, along with a sticky

After extensive research through various online resources, I have yet to find a solution to my specific CSS layout inquiry regarding "3 equal-height columns with a 'really' sticky footer". The desired layout includes the following components: ...

Installing the error package resulted in the following error: "Error: module 'nopt' not found."

After attempting to install node modules, I encountered the error message "Error: cannot find module 'nopt'." I tested various solutions, but none of them seemed to work. The image below shows the attached error message. { "name": "server", ...

Problem with Google's PageSpeed Insights - Focus on Making Most Important Content Visible

During the process of creating a comprehensive website for a client who has a strong affinity towards Google tools and recommendations, I have encountered an interesting challenge: Despite my best efforts, I seem unable to attain a flawless score for the ...

Modify the specified pattern with regex only if it does not appear in a particular location in the content

Our tool development heavily relies on regex for various functionalities. One key aspect involves replacing placeholders with actual values dynamically using standard JavaScript's `RegExp`. All placeholder formats are similar to this: {{key}} In mo ...

Controller method remains inaccessible despite multiple attempts with Javascript ajax Get call

I've tried numerous solutions for this issue, but I'm still unable to make it work. My goal is to invoke a controller method that accepts a parameter and returns a string based on that parameter. Despite using an ajax GET request, the outcome is ...

Avoid TypeError: cannot read property '0' of undefined in a Vue.js/Django project

I am currently working on a Django/Vue.js application. Upon submitting the login form, the Django view redirects to the user's username page, where the Vue.Js file retrieves data from the server. Below is the code snippet: async created(){ await ...

Exploring the differences between using a local controller in Angular's MdDialog modal versus displaying a shadow at

I've been attempting to utilize an app-level controller to showcase a modal dialog. The local function controller tests are functioning flawlessly, but the app level controller is only showing a grey shadow instead of the expected dialog. The edit an ...

Update a table in Laravel view using AJAX with a table join operation

How can I access the "nombreSubdireccion" attribute from the "subdireccion" table when inserting/updating a new record in the "area" table using AJAX? Currently, I am only able to get it by reloading the page due to DB::table. I'm unsure of where to d ...

Unable to find the Popper component in Material UI

Material-UI version "@material-ui/core": "^3.7.0" I have a requirement to display Popper on hover over an element, but unfortunately, the Popper is not visible. This section serves as a container for the Popper component. import PropTypes from &apos ...

Instructions on how to insert the meta tag with the attribute "http-equiv" set to "REFRESH" and the content "0; URL="somedomain"" into a division on a

Trying to send an ajax request to a page that includes the following meta tag: <meta http-equiv="REFRESH" content="0; URL=https://www.ohterDomain.com/help?nodeId=2&view=content-only"> After making a successful ajax call, the correct content is ...

what is the best way to align text with an image using CSS

Is there a way to display a form within an image without making it messy with additional tags and text? Below is the code that I currently have: .box { width: 650px; padding-right: 15px; /* creates gap on the right edge of the image (not con ...

Sending the HTML input value to a Knockout view

Can someone assist me with a dilemma I'm facing? Within CRM on Demand, I have a view that needs to extract values from CRM input fields to conduct a search against CRM via web service. If duplicate records are found, the view should display them. Be ...

"Missing the import of material-ui search icons is causing a functionality issue

import React from 'react' import {Search} from "@material-ui/icons/Search" const App = () => { return ( <div> <Search/> </div> ) } export default App The exported component 'Search' (impor ...

Element in list does not cover entire ul

I'm having trouble getting my list elements to span the entire width of the unordered list. I've tried applying styles to the nav ul li and setting the width to 100%, but nothing seems to work. How can I achieve this? .nav-container { border- ...

I updated the script to include a feature that automatically adds a leading zero to hours, minutes, and seconds if they are less than 10. However, for some reason, the output still doesn't show the leading zero

I have successfully created a countdown timer that works effectively. One of the conditions I added is to display leading zeros for hours, minutes, and seconds if they are less than 10. The desired format is like this (06 : 08 : 09) instead of (6 : 8 : 9 ...