When the button is clicked in CSS and JSP, a disappearing div is instantly replaced by a new div

I am brand new to Web programming. I have just grasped the concepts of HTML and CSS, but Java Script is still a mystery to me.

The scenario I am facing is as follows - there are two buttons and two divs. When one button is clicked, the corresponding div appears (visibility:visible). Clicking the same button again will make the div disappear. However, if I click both buttons simultaneously, the two divs overlap each other.

Now, I am looking to create an effect where if one button is already clicked (one div is visible), and then another button is clicked, the first div disappears and the other div appears in its place at the same time.


    <script>

    function show1() {
        document.getElementById("div1").classList.toggle("show");
    }
    window.onclick = function(event) {
        if (!event.target.matches('.button1'))
            var dropdowns = document.getElementsByClassName("div1_pop");
    }
    function show2() {
        document.getElementById("div2").classList.toggle("show");
    }
    window.onclick = function(event) {
        if (!event.target.matches('.button2'))
        var dropdowns = document.getElementsByClassName("div2_pop");
    }

    </script>

Java Script is still a challenge for me, so I simply copied and pasted this script section. Can someone help me achieve the desired appear/disappear effect by fixing this script?

Answer №1

One way to solve this issue is by displaying the first div while hiding the second one.

function displayFirst() {
    document.getElementById("div1").style.display = "block";
    document.getElementById("div2").style.display = "none";
}
window.onclick = function(event) {
    if (!event.target.matches('.button1'))
        var dropdowns = document.getElementsByClassName("div1_pop");
}
function displaySecond() {
    document.getElementById("div1").style.display = "none";
    document.getElementById("div2").style.display = "block";
}
window.onclick = function(event) {
    if (!event.target.matches('.button2'))
    var dropdowns = document.getElementsByClassName("div2_pop");
}

</script>

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

Warning: Node 125008 has reached the maximum number of listeners, indicating a potential memory leak in the EventEmitter

(node:125008) MaxListenersExceededWarning: There may be a memory leak with EventEmitter as 11 ready listeners have been added. Try using emitter.setMaxListeners() to raise the limit Can anyone provide guidance on how to increase the listener event count? ...

Increment counter when a key is pressed in Angular 4 and decrement when the backspace key is pressed

As a beginner in Angular, my goal is to create a feature where: The character count in a label increases as text is entered into a textarea. When the backspace key is pressed, the counter should decrease. If the user attempts to enter more characters tha ...

Having issues with Jquery and duplicating tables functionality not functioning as expected

I am facing an issue with two external jQuery files. One file allows me to clone the last row of a table, while the other is supposed to retrieve the id of a select tag based on a class assigned to it. However, the second script only works for the original ...

Ways to dynamically update the value of an object property within reactJS state

In the scenario where a component holds state like so: this.state = { enabled: { one: false, two: false, three: false } } What is the proper way to utilize this.setState() in order to set the value of a dynamic property? An attempt such ...

Utilize the Link_to function within a Rails jQuery script

Trying to incorporate link_to (<%= link_to 'address' , boat_path("'+boat_id+'") %>) within my jQuery code has presented a challenge due to concatenation issues preventing it from functioning as intended. Below is the jQuery code ...

Generating random numbers from an array can be done while excluding certain specified numbers

I'm currently developing a game where players are required to choose 6 numbers between 1 and 36. My goal is to create a new array that does not include the 6 selected numbers, resulting in an array that is 30 numbers long. Here is my approach: The ...

Combine TypeScript files in a specific sequence following compilation

I am hoping to utilize gulp for the following tasks: Compiling TypeScript to JavaScript, which is easily achievable Concatenating JavaScript files in a specific order, proving to be challenging Since I am developing an Angular application, it is crucial ...

Limit the vertical movement in Vue drag and drop operations

Currently, I am working on a project that involves implementing drag-and-drop functionality using vue-draggable. You can find more information about it here: https://github.com/SortableJS/Vue.Draggable. I am facing an issue where the height of the element ...

I am facing a dilemma where React Native seems to be disregarding all the container styles I

Here is the code snippet I am working on: import React from 'react'; import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'; export default class ShortcutsHome extends React.Component { render() { return ( ...

Is an array considered sparse when defined as 'array[i]=undefined'?

Are the effects of delete array[i] and array[i] = undefined identical? I'm aware that using the former can result in a sparse array - but does the latter have the same outcome? ...

Issues with the POST API functionality in an express / node.js application when using mysql database

Recently, I have been diving into learning nodejs and experimenting with creating a post API that interacts with static data stored in a MySQL database. Let me briefly share the configuration setup I am working on: const mysql = require('mysql') ...

Could a class method be accessed from outside a nested handler method in JavaScript?

I am trying to make an XMLHttpRequest to a server using JavaScript. In the handler function, I need to call a method from the surrounding class. Is there a way to achieve this? Understanding how this works in JavaScript can be confusing. I have experiment ...

Create an array in JavaScript using the JSON data that was returned

After receiving some JSON data from a REST call, I have successfully parsed and added totals to the data. The results can be viewed on the page (refer to this post: json sibling data). However, now I want to further break down the totals. Here is the initi ...

Using JSON.stringify() for custom serialization of an object

Imagine there is an object called a with properties: const a = { foo: 123, bar: 'example' } Now, this object is a part of another object called b like this: const b = { a: a, anotherField: "example" } While working with TypeScript, th ...

What steps should I take to repair my image modal button?

Within my application, I have an image modal placed inside a header tag, along with a label. There is some space between the label and the image modal. However, when the application is run, the page appears in a fixed position, but the label message does n ...

Rows in the table are distinguished by varying colors

Is it achievable to replicate this design solely using table CSS style? I am able to assign different colors to even and odd rows, but my goal is to achieve something like this (just the colors): ...

Multiple file uploads with dynamic image previews and individual names

Currently, I'm facing an issue with displaying a preview and the name of images before uploading via ajax. To show the preview, I am using the File Read API along with the ".name" method to display the file name. However, all the previews are showing ...

"Selecting elements with CSS using the 'element'

While exploring some websites for studying purposes, I came across this code snippet. .site-header .widget-area span.but As a beginner in CSS, I'm curious to know more about the span.but part. Is it similar to the :not pseudo selector? ...

Errors in ajax are displayed only if the request fails, they are not shown if

I have been using a request form that was functioning properly until today when it suddenly stopped working correctly. Now, when I submit the form, any errors that occur are displayed on the screen and in the network. However, if the form is submitted with ...

Exporting from Blender as an OBJ file and importing it into Three.js seems to be causing issues with the mesh normals

I exported my model from blender to OBJ format and then imported it into Three.js. The normals for the wheels appear to be facing inward. As for the track, only the mesh is visible and it does not seem to be correctly mapped. Upon reimporting the OBJ fi ...