Issue with hiding and showing two divs using Jquery

Whenever the page is refreshed, I notice that my second <div> is displayed, which I would like to prevent.

$(function(){
    $("#div2").hide();
    $("button").click(function(){
        $("#div1").hide()
        $("#div2").fadeIn(1000)
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div1">
    Lorem, ipsum dolor sit amet consectetur adipisicing elit. Doloribus sequi omnis veritatis dicta laborum iste provident, ex ratione, neque veniam quis. Hic repellat nulla in minus! Sit quia dicta modi.
    <button id="button">Click</button>
    
  </div>
    
    
  <div id="div2">
    Lorem, ipsum dolor sit amet consectetur adipisicing elit. Doloribus sequi omnis veritatis dicta laborum iste provident, ex ratione, neque veniam quis. Hic repellat nulla in minus! Sit quia dicta modi.
</div>

Answer №1

Don't use the hide() function to conceal div2, utilize CSS instead.

#div2 { display:none; }

Also, make sure to include the missing ; in your script code:

 <script>
$(function(){

    $("button").click(function(){
        $("#div1").hide();
        $("#div2").fadeIn(1000);
    });

});
</script>

Answer №2

 <!doctype html>
 <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
       <script>
       $(document).ready(function() {
         $("#content2").hide();
         $("button").click(function(){
         $("#content1").hide()
         $("#content2").fadeIn(1000)
        });
        });
        </script>
 </head>

 <div id="content1">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus sequi omnis veritatis dicta laborum iste provident, ex ratione, neque veniam quis. Hic repellat nulla in minus! Sit quia dicta modi.
    <button id="button">Click Me</button>
 </div>

 <div id="content2">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus sequi omnis veritatis dicta laborum iste provident, ex ratione, neque veniam quis. Hic repellat nulla in minus! Sit quia dicta modi.
 </div>
 </html>

Answer №3

Using jQuery is all you need to make your code work smoothly

$(document).ready(function() {
    $("#div2").hide();
    $("#button").click(function(){
        $("#div1").hide();
        $("#div2").fadeIn(1000);
    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="div1">
    Lorem, ipsum dolor sit amet consectetur adipisicing elit. Doloribus sequi omnis veritatis dicta laborum iste provident, ex ratione, neque veniam quis. Hic repellat nulla in minus! Sit quia dicta modi.
   
    
  </div>
     <button id="button">Click</button>
    
  <div id="div2">
    Lorem, ipsum dolor sit amet consectetur adipisicing elit. Doloribus sequi omnis veritatis dicta laborum iste provident, ex ratione, neque veniam quis. Hic repellat nulla in minus! Sit quia dicta modi.
</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

Utilizing a dynamically created Stripe checkout button

Currently, I am attempting to integrate a checkout button from the Stripe Dashboard into my VueJS Project. I have a feeling that I might not be approaching this in the correct manner, so if you have any advice, I would greatly appreciate it. In order to ...

What is the best method for retrieving information from MongoDB and presenting it in a table with Node.js?

I'm struggling to understand how to retrieve data from mongodb and display it in an html/ejs file. I have a button in the html/ejs file that, when clicked, should show all the data from a mongodb database collection. While I've come across simil ...

How does the method of including JavaScript libraries in HTML differ from adding them as npm dependencies?

Upon browsing through npm highly regarded packages, I noticed that popular projects such as Grunt, lodash, and underscore are readily available. I have always utilized these in the traditional manner: <script src="js/lib/lodash.min.js"></script& ...

I'm having trouble getting my <aside> HTML tag to align properly within my grid section

I'm struggling with setting up grids. I have defined the boundaries and everything seems to fall into place within the grid, except for my aside element that I want to appear on the right side of the screen. Interestingly, this issue arises when using ...

Is it possible to modify the border colors of separate elements in Bootstrap 5?

<div class="m-3 p-3 border-5 border-top border-danger border-bottom border-primary"> Can borders be styled with different colors? </div> Is there a method to achieve this, possibly using sass? ...

Tips for altering the distance between dots on a line

.ccw--steps { margin: 10px auto; position: relative; max-width: 500px; } .ccw--step-dot { display: inline-block; width: 15px; border-radius: 50%; height: 15px; background: blue; border: 5px solid #e8e8e8; position: relative; top: -8p ...

Changing the color of the selected item in a Jquery Mobile ListView

I have a dynamic list that triggers a JavaScript function using the onclick event. However, I am struggling to change the color of the selected item in the list. Here is an example of the code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional ...

Deciphering the LocalDate and nested object array in an AJAX response

Seeking assistance, looking for solutions to two problems. Firstly, how can I display LocalDate in an ajax response? And secondly, how do I iterate over a list of Custom objects received in the ajax response? I am passing a List of Custom Objects and Loca ...

Swapping mouse cursor using JavaScript

Currently, I am working on a painting application in JavaScript that utilizes the Canvas Object. I would like to customize the mouse cursor when it hovers over the Canvas object. Can anyone advise me on how to accomplish this? ...

Variable in v-for loop is not properly declared

Currently, I am attempting to iterate through an array of objects retrieved from the backend and display these objects on the frontend. The Vue framework is throwing an error stating that "event" is not defined on the instance but referenced during render. ...

jQuery eliminates initial div just a single time

Here is a function I have created: function removeDiv() { var topmost = jQuery('.xx'); var totContent = topmost.find('.zz').length; var $target = jQuery('.xx').find('.zz').eq(0); if(totCont ...

The submission directed me to the PHP webpage

Trying to integrate AJAX and jQuery into my code has been a challenge. Whenever the form is submitted, it redirects me to the PHP page in the action attribute. I'm not sure why this keeps happening, and I've been struggling to figure it out. The ...

Can you explain the concept of a TransientTransactionError within Mongoose (or MongoDB)?

Two important files in my project are server.js and db.js. The db.js file is responsible for interacting with the database using Mongoose, while server.js calls functions from db.js: var mongoose = require('mongoose'); mongoose.connect('&ap ...

What's causing the problem with my custom filter?

I've encountered an issue with a custom filter I'm currently working on. The challenge lies in the fact that I'm using Angular 1.3.6 and am unable to upgrade at the moment. Therefore, I truly need your assistance with this matter. Check out ...

Managing class values with Selenium - Manipulating and updating classes

In the program I'm working on, there is a need to toggle which element receives the class value of "selected". <div class="countryValues"> <div data-val="" >USA and Canada</div> <div data-val="US" >USA - All< ...

Understanding Node.JS: A Dive into Key Concepts

Forgive my lack of knowledge, but I'm really trying to grasp the differences between Node.js and Backbone.js. I believe I'm getting there, but could someone confirm this or guide me in the right direction? Node.js is a platform that can handle H ...

I'm having some trouble with jQuery's GET function while working with CodeIgniter. Instead of retrieving a single value, it seems to be returning the source code for a

I have been experimenting with the jQuery GET method, but the results are not what I expected. My goal is to call a function from my controller using jQuery and display the returned value in a specific div within my view. Below you can see the code snippet ...

Linking a script to a button in ASP .NET MVC Razor View

I'm struggling to get a button to invoke a script. Here's the button and script: <script> function SubmitClick () { var pid = $(this).data('personid'); var sid = $(this).data('surveyid'); v ...

Tips for sending jQuery variable with an Ajax GET call?

I have a jQuery slider that generates values. Now, I need to pass these variables to a URL in the format using an Ajax get request. In my index.php file, I have the following JavaScript code which assigns values to the variables 'rich' and &apo ...

Leverage closures within Underscore.js templates for enhanced functionality

Is there any benefit to utilizing a closure in an underscore template for purposes such as keeping track of counters? Here's a simple example: <% (function( models ){ var length = models.length-1, section = ""; _.each( models, functi ...