Prevent the Button from causing the Aspx Page to refresh

I've been working on a JavaScript function for my website that displays a popup when a button is clicked. However, every time I click the button, the webpage reloads causing the popup to disappear. Is there a way to prevent the page from refreshing when I click an HTML button without interfering with the functionality of my ASPX buttons?

Here's the HTML/ASPX code:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link rel="stylesheet" type="text/css" href="../css/main.css" />
    <script type="text/javascript" src="../Scripts/jquery.js"></script>
    <script type="text/javascript" src="../Scripts/jqueryui.js"></script>

    <script type="text/javascript" src="../Scripts/myjqTest.js"></script>

    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
    <div id="header">
    <li><asp:LinkButton ID="loginoptionsButton" CssClass="loginoptionsButton"
    runat="server" CausesValidation="False" ClientIDMode="Static"
    OnClick="loginoptionsButton_Click">LOGIN</asp:LinkButton></li>
    <li><asp:LinkButton ID="signuplogoutButton" CssClass="signuplogoutButton"
    runat="server" CausesValidation="False" ClientIDMode="Static"
    OnClick="signuplogoutButton_Click">CADASTRAR-SE</asp:LinkButton></li>
    </div>

    <div id="content">
        <button id="TestJS" class="">Test</button>
        <div id="Popup" class=""></div>
    </div>
</form>
</body>
</html>

And here's the JavaScript code:

var test = document.getElementById("Popup");

test.addEventListener("click", function () {
    if (test.className === "Hide") {
        test.className = "";
    } else {
        test.className = "Hide";
    }
});

Answer №1

Here is a suggestion for you:

test.addEventListener("click", function (event) {
    if (test.className === "Hide") {
        test.className = "";
    } else {
        test.className = "Hide";
    }
    event.preventDefault();
});

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

Parsing JSON data using a regular expression

Within my JavaScript file lies a plethora of object literals: // lots of irrelevant code oneParticularFunction({ key1: "string value", key2: 12345, key3: "strings which may contain ({ arbitrary characters })" }); // more irrelevant code My ta ...

php code to paginate mysql results

Imagine I have 50 rows in my database. How can I retrieve MySQL results in pages, displaying 5 results on each page and showcasing the pages as follows: [1], 2, 3, 4...10? For example, if it's on page 5, show 3, 4, [5], 6, 7...10 without refreshing al ...

Angular JS - A guide to implementing ng-model binding at a later stage

In a large codebase, there is a straightforward scenario that I need help with: There is an Angular APP and Controller already set up and functioning correctly. However, a ng-model element needs to be dynamically created at a later stage (perhaps through ...

The issue with scaling SVG files imported via Babel in Next.js is still

I've been attempting to decrease the size of my SVG icon in my next.js project. To import SVGs into my project, I included a file called .babelrc with the following: { "presets": ["next/babel"], "plugins": [" ...

There was a TypeError that occurred because the object did not have the 'ajax' method available

When attempting to initialize the quoteResults Javascript function on my Wordpress site, I encounter the following error in my console: Uncaught TypeError: Object #<Object> has no method 'ajax' I have successfully utilized the code below ...

When attempting to pass a token in the header using AngularJS, I encounter an issue with my NodeJS server

Error is: Possibly unhandled Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11) at ServerResponse.res.set.res.header 1. All Node.js services were functioning properly before ...

Adjust the size of an image with jquery without relying on server-side scripts

I am currently developing an application for Samsung Tizen TV that displays images from live URLs. On one screen, there are approximately 150 images, each with a size of around 1 MB and a resolution of 1920 by 1080. Navigating between these items has bec ...

Tips for transforming a nested object into an array using JavaScript

I have a complex array containing multiple objects, each with two or more sub-objects. I am looking to consolidate all sub-objects into a single array of data using JavaScript. How can I achieve this? var array1 = [ { "dfgasg24":{ name:"a", ...

Steps for performing a runtime cast

When working on a web application written in TypeScript, there is a feature where users can add additional JavaScript functions that will be parsed at runtime (new function(Function as String)) for execution. These functions should return an object defined ...

What is the comparison between actual pixels and text size in Internet Explorer?

Can you determine the actual font size in pixels corresponding to the following text size options in Internet Explorer? Largest Larger Medium Smaller Smallest In a web development project, I am looking to achieve a similar functionality to adjust the te ...

The click-handler method in VueJS Paginate Component fails to activate

I'm currently working on a Vue Component code that involves pagination for a list. The pagination seems to be working fine, except for the issue I encounter when trying to navigate to the next page, which in this case is page 2. I've added a cons ...

How can you implement a repeating carousel with JavaScript?

I recently came across some carousels on and was intrigued by how they smoothly repeat the slides. Instead of jumping back to the first slide when reaching the end, it seamlessly transitions from the final slide back to the first one. I am eager to learn ...

"Centering an Image in ASP.NET HyperLink Control: A Step-by-Step

Here is the setup for my control: <asp:HyperLink CssClass="link-image" ID="Link1" runat="server"/> Code-behind in C#: Link1.ImageUrl = "imagePath"; Resulting HTML code: <a class="link-image" id="MainContent_Link1"> <img alt="" src=" ...

Oops! RangeError [MESSAGE_CONTENT_TYPE]: The content of the message must be a string that contains at least one character

Can someone help me troubleshoot my regular send command? I keep encountering an error message even after following suggestions from previous answers. Here is the error: RangeError [MESSAGE_CONTENT_TYPE]: Message content must be a non-empty string. at ...

The periodLookup array does not have a defined value for periodStr. Why is this error being caught?

Here is a method that I am working with: set_period_help_text: function(periodInput){ var metric = MonitorMetric.getSelectedMetric(); var periodStr = $('select[name=metric_period]').val(); var datapoints = Number(periodIn ...

Unable to direct to the main page in index.js of the next.js application

I have been working on a next.js application and encountered an issue with a component I created called <ButtonGroup>. I added a button with the code <Button href="index">Home</Button> to allow users to navigate back to the home ...

Creating a Navigation Bar in Outlook Style Using Javascript and CSS

Looking for a navigation sidebar design similar to Outlook for my web application. I have seen options available as Winform controls and through Visual WebGUI, but these are Microsoft-dependent solutions. We need a Javascript & CSS based solution that is s ...

Create an mp3 file using the arrayBuffer functionality

Struggling with StackOverflow, I attempted to record audio from a Raspberry Pi using Node.js (1). The audio stream is then sent through a WebSocket server (code omitted for simplicity), where a Vue.js WebSocket listens to the stream. My goal is to save thi ...

What could be causing my webpage to freeze every time a filter button is selected?

Tasked with developing a webpage similar to Pinterest by utilizing data from a JSON response. Each JSON object contains a service_name key, which can be manual, twitter, or instagram. I made an effort to implement three filter buttons to only display the r ...

Setting bootstrap datetimepicker values dynamically from a variable

I'm trying to utilize the inline bootstrap datetimepicker from At the moment, it opens in a popup window after the user selects a date/time from a table on another page. My goal is to pass the date/time from that page into datetimepicker and have it ...