Revealing content with a click on a div

I am in the process of creating a website and I want it to consist of just one index page. When visitors land on the page, they will see several rows similar to this example. By clicking on a row, the hidden information below will expand, giving it a look like this.

In my search for a solution, I came across this resource and attempted to implement it by copying and pasting the code into a new file for testing purposes. However, it did not work as expected even after adding the necessary HTML tags and files. Here is a snippet from my code:

//Index
<!DOCTYPE html>
<html>

    <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    <link href="main.css" rel="stylesheet">
    <script type="slide.js"></script>   

<div class="container">
    <div class="one">Click me to reveal new div</div>
    <div class="two">Hey it worked!
        <br>New Contenttt</div>
</div>
</html>

//main.css
 .container {
    overflow:hidden;
    height: 60px;
}
.one {
    position: relative;
    top: 0;
    background-color: #FFC300;
    z-index: 1;
    cursor:pointer;
}
.two {
    position: relative;
    top: -40px;
    background-color: yellow;
    z-index: -1;
    -webkit-transition: top 1s;
    -moz-transition: top 1s;
    -o-transition: top 1s;
    transition: top 1s;
}

//slide.jsvar clicked=true;
$(".one").on('click', function(){
    if(clicked)
    {
        clicked=false;
        $(".two").css({"top": 0});
    }
    else
    {
        clicked=true;
        $(".two").css({"top": "-40px"});
    }
});

Despite my efforts, the code does not seem to be working properly. Can anyone help me identify what I am missing?

Answer №1

let clicked = true;

This line of code is currently commented out. Please uncomment it.

I trust this information will be beneficial to you.

Answer №2

It seems that what you're searching for is something along these lines

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-3.6.0.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function (event) {
                $(".click-me").on('click', function(){
                    $(".reveal-div").slideToggle('fast')
                });
            });
        </script>

        <style>
             .container {
                overflow:hidden;
                height: 80px;
            }
            .click-me {
                background-color: #FF5733;          
                cursor:pointer;
            }
            .reveal-div {
                display:none; /* comment this line to show the div by default*/
                background-color: lightyellow;           
            }
    </style>
    </head>

    <div class="container">
        <div class="click-me">Click here to reveal hidden content</div>
        <div class="reveal-div">Success! 
        <br>New information has been revealed</div>
    </div>
</html>

Answer №3

Hey there, I'm not quite sure how your markup is structured based on your previous comments about messing up with copy and paste.

You mentioned, "I just messed up when copying and pasting."

Here is the complete markup including JS and CSS:

<!DOCTYPE html>
 <html>
  <head>
    <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
    <style>

 .container {
    overflow:hidden;
    height: 60px;
}
.one {
    position: relative;
    top: 0;
    background-color: #FFC300;
    z-index: 1;
    cursor:pointer;
}
.two {
    position: relative;
    top: -40px;
    background-color: yellow;
    z-index: -1;
    -webkit-transition: top 1s;
    -moz-transition: top 1s;
    -o-transition: top 1s;
    transition: top 1s;
}

 </style>

 </head>
 <body>
    <div class="container">
      <div class="one">Click me to reveal new div</div>
      <div class="two">Hey it worked!
        <br>New Contenttt</div>
    </div>
    <script>
       var clicked=true;
       $(".one").on('click', function(){
            if(clicked) {
                clicked=false;
                $(".two").css({"top": 0});
            } else {
                clicked=true;
                $(".two").css({"top": "-40px"});
            }
        });
   </script>
  </body>
</html>

NOTE: I have included the body and head tags. Also, please clarify if you are referencing the CSS and JS from external files. Currently, I am referring to both the CSS and JS within the same page.

Check out the working demo here!

Answer №4

Modification

<script src="//code.jquery.com/jquery-1.12.0.min.js"></script> should be replaced with <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> in the index.html file 

Also, replace

<script type="slide.js"></script>
with
<script src="slide.js"></script>
and ensure your code is enclosed within
$(document).ready( function () { });
Additionally, uncomment the line var clicked=true;

The updated code will look like this:

index.html

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <script src="slide.js"></script>
    <link href="main.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <div class="one">Click me to reveal new div</div>
        <div class="two">Hey it worked!
            <br>New Contenttt</div>
    </div>
</body>

slide.js

$(document).ready(function() {
var clicked = true;
$(".one").on('click', function() {
    if (clicked)
    {
        clicked = false;
        $(".two").css({"top": 0});
    }
    else
    {
        clicked = true;
        $(".two").css({"top": "-40px"});
    }
});

});

main.css

.container {
overflow:hidden;
height: 60px;
}
.one {
position: relative;
top: 0;
background-color: #FFC300;
z-index: 1;
cursor:pointer;
}
.two {
position: relative;
top: -40px;
background-color: yellow;
z-index: -1;
-webkit-transition: top 1s;
-moz-transition: top 1s;
-o-transition: top 1s;
transition: top 1s;
}
/*.one:hover + .two {
top: 0px;
}*/

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

Responsive functionality in Twitter Bootstrap div layering

My website features a hero unit with a form displayed on top. Behind the form, a series of images fade in and out smoothly one after another. I'm attempting to overlay the div containing the form over the div with the background images by setting the ...

What is the process for submitting and storing a collection of files in a database as a list?

I am trying to implement a feature in my MVC project where users can upload multiple files and see them listed before submitting the form. However, I am facing some challenges with finding a solution for this. Demo: My goal is to allow users to add multi ...

Use jQuery to modify the default yellow color of Chrome's autofill feature

Struggling to get this code to work on my website, and it's starting to drive me crazy. I followed the instructions from BenjaminMiles' website, but something just isn't clicking. The jquery code snippet I'm using is: <script> ...

Utilizing the Jquery click function to assign an element as a variable

I'm currently working on a script that aims to extract the inner text of any clicked item with the class "customclass". Keep in mind that this specifically targets anchor elements. However, I've encountered an issue where the individual element ...

Prevent scrolling on the entire page, except for specific div elements that are taller than the browser window

I've scoured all corners of the internet in search of a solution to my dilemma. Here's the situation: I have a one-page website with multiple div elements stacked beneath each other, almost like separate pages. What I'm aiming for is to comp ...

What steps do I need to take in order to integrate an mpg video onto my

I am in need of embedding mpg (dvd compliant mpeg2) movie files onto my webpage. Unfortunately, I do not have the ability to convert these videos into any other format. This webpage is solely for personal use, so any solution would be greatly appreciated. ...

The jquery scrolltop() function provides the value of the scroll position prior to the current

Using the jQuery function element.scrollTop(), I attempted to retrieve the current scroll position of the page with the following line of code: var currentScrollPosition = $('html').scrollTop() || $('body').scrollTop(); However, this ...

No specification has been provided for the delivery

I'm currently working with the Meteor framework and I am attempting to send an uploaded file (HTML input) from the client to the server using the npm package Delivery. Below is my code: Client side : var socket = io.connect('http://0.0.0.0 ...

What are the benefits of using "var self = this" for synchronizing between a class and events?

Consider this straightforward code example (it's in AngularJS for simplicity, but the scenario is common in JavaScript): angular.module('app',[]). directive('myDir', function(){ this.state = {a:1, b:2}; return { l ...

Changing Value in jQuery Dropdown Menu

I currently have two JQuery UI dropdowns. When I select an option in the first dropdown and set a checkbox to true, I want to copy that selected value to the second dropdown using the .change() event of the checkbox. To retrieve the selected value, I use ...

Caption image on hover

Is it possible to make an image overlap text on a horizontal menu bar when hovering with the mouse? I am designing a horror website and would like a bloody handprint to appear over the links in the menu bar when they are hovered over. I know this can be do ...

Incorporate active styling and apply CSS to the links <a> within Bootstrap buttons

Hello everyone! I am currently learning how to use bootstrap/CSS as I work on designing a website for one of my courses. Check out the Heroku app here At the moment, I have successfully created my header. It includes some buttons wrapped in <a> tag ...

Is there a way to retrieve the spring model object within the Ajax success function?

I am attempting to make an Ajax GET request to a specific endpoint. After the request is successful, I need to handle a conditional logic based on the "statusCode" that was set in the model. Below is the code snippet: Controller code: @RequestMappi ...

Problem with sending JSON-encoded arrays to an API

After successfully connecting to an API via Postman, I encountered an issue with my AJAX call. The error message returned is: Object {status: false, message: "Add amenities in JSON string"} The problem seems to be related to the amenities parameter, as ...

Validating usernames using Jquery's remote method检verifying the username

Struggling with the validation plugin and Laravel 4.2. I've been attempting to use ajax to check the username, but it's not functioning as expected. No matter if the username exists or not, it always appears available. Another problem arises whe ...

Is there an efficient method for transferring .env data to HTML without using templating when working with nodejs and expressjs?

How can I securely make an AJAX request in my html page to Node to retrieve process.env without using templating, considering the need for passwords and keys in the future? client-side // source.html $.get( "/env", function( data ) {console.log(data) ...

Is it possible to modify the names of the months in bootstrapMaterialDatePicker?

I've been struggling to change the month name in my code, and despite trying multiple approaches, it still doesn't work. Here is a snippet of my code: HTML; <div class="form-line"> <input type="text" class="form-control datepicker" ...

Angular throws an error when trying to parse undefined data outside of an async function

I'm having trouble parsing data retrieved from an http call and passing it to ngOnInit. Can you assist me in finding a solution? My project is built with Angular 4. Here's the async function: async getAsyncData() { this.asyncResult = awai ...

Is it possible to author TypeScript modules in a format other than ES6?

Is it possible to utilize AMD for writing code? define([ 'hb!./some/file.hb' ], function(template) { // }) ...

Apply a unique CSS template to an element while using directives

I'm in the process of creating a dynamic menu that will load menu items from a JSON file. When a menu item is clicked, I want to apply a different template to it. My initial plan was to use Directives, but now I'm trying to figure out how to inc ...