The button on my screen is not responding when clicked and keeps throwing errors

I have implemented a login form within a hidden div, and upon clicking the "Login" button, I want the form to be displayed. This functionality is achieved through a JavaScript function that I created to be triggered on onclick event of the Login button. However, when I click on the button, I encounter these errors,

Uncaught ReferenceError: showDiv is not defined at HTMLButtonElement.onclick ((index):72:29)

Despite looking into various solutions for this issue, nothing seems to resolve it for me.

Here is my code:

@section Scripts {

    <script>
        function showDiv() {
            console.log("pressed");
            document.getElementById("login-popup").style.display = "flex";
        }
    </script>
    @{
        await Html.RenderPartialAsync("_ValidationScriptsPartial");
    }
}
#login-popup {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 9999;
}

#login-popup-content {
    background-color: #fff;
    padding: 20px;
    border-radius: 5px;
}

    #login-popup-content h2 {
        text-align: center;
    }

    #login-popup-content .form-group {
        margin-bottom: 10px;
    }

    #login-popup-content input[type="text"],
    #login-popup-content input[type="password"] {
        width: 100%;
        padding: 5px;
    }
<button onclick="showDiv()">Login</button>
<div id="login-popup">
    <div id="login-popup-content">
        <h2>Login</h2>
        <form asp-action="Login">
            <div id="form-group">
                <input asp-for="@Model.LoginVM.Username" placeholder="Username" required />
                <span asp-validation-for="@Model.LoginVM.Username"></span>
            </div>
            <div id="form-group">
                <input asp-for="@Model.LoginVM.Password" placeholder="Password" required />
                <span asp-validation-for="@Model.LoginVM.Password"></span>
            </div>
            <div id="form-group">
                <input type="submit" value="Login" />
            </div>
        </form>
    </div>
</div>

EDIT:

This is how my _Layout looks like.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - The Burning Apron</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
    <link rel="stylesheet" href="~/TheBurningApron.styles.css" asp-append-version="true" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<partial name="Login">
<div id="navbar">
        <ul>
            <li>
                <a href="/index">Home</a>
            </li>
            <li>
                <a href="/recipe-categories">Recipes</a>
            </li>
            <li>
                <a href="/favorites">Favorites</a>
            </li>
            <li>
                <a href="/about-us">About us</a>
            </li>
        </ul>
        <br />
        <br />
        <hr />
    </div>
    @RenderBody()
    <footer class="border-top footer text-muted">
        <div class="container" style="color: black">
            &copy; 2023 - The Burning Apron -
        </div>
    </footer>

    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>

    @RenderSection("Scripts", required: false)
</body>
</html>

Answer №1

Give it a shot

<button onclick="displayDiv">Sign In</button>

(excluding the brackets)

As soon as the HTML page loads, JavaScript kicks in and executes displayDiv(), which is a function call. This means that the function is called and its output is sent to the onclick-handler. By altering it to displayDiv, you are now passing a reference of the function to the onclick handler, fulfilling your desired outcome.

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

I believe there may be a gap in the communication between TypeScript, JavaScript, Angular, Nginx, Alpine, and Docker within the network using Nginx. I am

After transitioning to Docker in order to create a virtual network to mimic a real network (using a bridge type with DNS that resolves the FQDN correctly to the corresponding IP), I encountered the following errors in the console.log - no data is being dis ...

"Enhancing user experience with real-time suggestions using

Try to: $(document).ready(function () { $('#cityName').autocomplete({ source: function(request,response) { $.ajax({ type: 'POST', url: '@Url.Action("Searc ...

What is the best way to send a string to the server and then pass it back to a component as props in Next.js?

Forgive me for my naive question, but I am new to Next.js and have been tasked with inserting the string "Hello from SSR" into a server log and passing it back to MyWonderfulComponent. Any suggestions on how to accomplish this? Here is the code example: e ...

Converting a TypeScript class to a plain JavaScript object using class-transformer

I have a few instances of TypeScript classes in my Angular app that I need to save to Firebase. However, Firebase does not support custom classes, so I stumbled upon this library: https://github.com/typestack/class-transformer which seems to be a good fit ...

A glitch occurred while attempting to load content dynamically onto an HTML page using Ajax

Attempting to dynamically load content into an HTML div is causing issues for me. Whenever I try to do so, an error pops up: Syntax error HOME. (this is the expected visible content within the div). HTML: Navigation bar: <li><a href="#" onclick= ...

Issue with setting .mtl properties in a custom shader in three.js

In my custom three.js application, I am loading an OBJ/MTL model for rendering. I am trying to apply a custom shader to the model, but the color and specular uniforms that I manually pass to the RawShaderMaterial are not updating correctly. Instead, they a ...

Guide on Incorporating Coffeescript into the Node.js Blueprint Framework

Have you checked out Skeleton (https://github.com/dstroot/skeleton) yet? It appears to be a robust framework for node.js, offering all the middleware you need. However, it seems to lack support for coffee script. How can we incorporate it into our project? ...

Steps for adding an image to Firebase

I recently started a project using React and Firebase. After going through some tutorials, I managed to add and read text from Firebase successfully. Now I am working on displaying data like name, estate, and description in a modal on my React Firebase. M ...

Is there a way to update the value of a variable with the help of a checkbox?

When I check the checkbox, the specOrder const gets updated as expected. However, I am struggling to figure out how to remove the value when the checkbox is unchecked. Below is the code I have been working on: const SpecialtyBurgers = () => { cons ...

Please choose only one selection from each of the two dropdown menus

I spent an entire day searching for the correct answer, but to no avail. The issue I am facing is with two dropdowns. What I need is that if a user selects an option from the first dropdown, the choice in the second dropdown should revert to default. Simi ...

Tips for making nested sliding divs within a parent sliding div

Is it possible to slide a float type div inside another div like this example, but with a white box containing "apple" text sliding inside the black div it's in? I have attempted to recreate the effect using this example. Here is my current JavaScript ...

How to effectively handle form CRUD operations in Angular 2/4 using parent and child components

In my setup, there is a parent component along with a child component. The parent mostly consists of a list of repeating children created using *ngFor directive. Each child component receives an object through Angular's @Input feature, and the child ...

Clicking on a link does not place the focus on the corresponding input field

My project involves creating a One Pager website with a navigation menu. I am looking to implement radio inputs that are associated with the clicked link (a) so that I can apply CSS styles to the active radio button. Currently, the CSS is applied when the ...

Filter in AngularJS to display a different value retrieved from the database depending on the current iteration value

I am in the process of migrating an existing Laravel (and jQuery) application to Angular/Laravel. Here is a snippet from my code: <td>{{ Person::byId($record->id) }}</td> During my iteration, I called a function to retrieve a name based o ...

Getting Node.js to function seamlessly with tree-model-js

Just wanted to say a big thank you for tree-model-js, it's an amazing library! I'm fairly new to Node.js and I've been trying to incorporate the tree-model into my project. Successfully npm'ed everything. As I attempt to integrate Node. ...

Goodbye.js reliance on lodash

In my search for the most lightweight and speedy jQuery implementation for server-side NodeJs, I've come across Cheerio as the best option. However, I've noticed that Cheerio has a code-size of around 2.6 MB, with approximately 1.4 MB attributed ...

Is there a way to utilize JQuery to swap out a CSS background image while preserving the gradient effect?

Currently, I am facing a challenge in dynamically updating the background-image property of a div while preserving other background-related properties (such as color and gradient) using jQuery's .css() function. Although I can successfully replace the ...

Calling the ColdFusion component with $.ajax results in returning plain text instead of HTML

Apologies if this question has already been addressed. I am working on setting up a pagination system that involves sending variables to a ColdFusion function query in order to fetch a specific number of elements and display those records on a web page wi ...

Verify input submission using jQuery UI

I'm in the process of verifying a submit form that has been created using Ruby on Rails. Prior to submitting, I have implemented a condition that triggers a confirmation popup asking the user if they truly wish to proceed. Initially, this was function ...

What is the best way to attach a function to an href attribute for creating a dynamic link?

I've been struggling to pass a function to the href attribute in my link tag, but no matter what I try, it just doesn't seem to work. It either redirects to a 404 page or is completely unclickable. What could be causing this issue? This link app ...