Show/Hide sections of content that transition and rearrange the layout of the page

I have a layout with 9 squares on a page. When clicking on a square, I want a full-width div to appear underneath it, pushing the other squares down.

I've tried following a tutorial to understand the JavaScript behind this functionality, but I'm having trouble getting it to work.

Here is the Codepen link for reference: http://codepen.io/SaraTez/pen/QEmPjX

<ul class="grid-list">
  <li class="li">One</li>
  <li class="li">Two</li>
  <li class="li">Three</li>
  <li class="li">Four</li>
  <li class="li">Five</li>
  <li class="li">Six</li>
  <li class="li">Seven</li>
  <li class="li">Eight</li>
  <li class="li">Nine</li>
  <li class="li">Ten</li>
  </ul>

JavaScript Code:

$(".grid-list .li").click(function(){
var .li=$(this);

if ($(this).hasClass("active")) {
    $(".grid-list .li.cloned-expand").remove();
    $(".grid-list .li").removeClass("active");
}
else {
    $(".grid-list .li.cloned-expand").remove();
    $(".grid-list .li").removeClass("active");
    var cloned=.li.clone().addClass("cloned-expand").append("<span  class='cloned-expand-close'>X</span>");
    .li.addClass("active").after(cloned);
    $('html, body').animate({
        scrollTop: $(this).offset().top
    }, 400);
}
});

$(document).on("click", ".cloned-expand-close", function(){
    $(".grid-list .li.cloned-expand").remove();
    $(".grid-list .li").removeClass("active");
});

While the squares appear correctly on the page, the click function is not working. I suspect there might be an issue with my JavaScript code or syntax. Any help in fixing this issue would be greatly appreciated. Alternatively, if you know of any examples showcasing the desired functionality, please share them.

Answer №1

It seems that the issue lies in the incorrect definition of your variable.

Revised definition:

var li=$(this);

    $(".grid-list .li").click(function(){
        var li=$(this);

        if ($(this).hasClass("active")) {
            $(".grid-list .li.cloned-expand").remove();
            $(".grid-list .li").removeClass("active");
        }
        else {
            $(".grid-list .li.cloned-expand").remove();
            $(".grid-list .li").removeClass("active");
            var cloned=li.clone().addClass("cloned-expand").append("<span class='cloned-expand-close'>X</span>");
            li.addClass("active").after(cloned);
            $('html, body').animate({
                scrollTop: $(this).offset().top
            }, 400);
        }
    });

    $(document).on("click", ".cloned-expand-close", function(){
        $(".grid-list .li.cloned-expand").remove();
        $(".grid-list .li").removeClass("active");
    });
* {
  box-sizing: border-box;
}
  .li {
    display: inline-block;
    width: 15%;
    height: 100px;
    line-height: 100px;
    border: 1px solid;
    margin: 0 1% 10px 0;
    text-align: center;
    position: relative;   
    cursor: pointer;
    &:hover {
      background-color: #eee;
    }
    .li:active {
        background-color: #eee;
        &:after {
          content: '';
          height: 0;
          width: 0;
          border-left: 10px solid transparent;
          border-right: 10px solid transparent;
          border-top: 10px solid #000;
          position: absolute;
          bottom: -10px;
          left: 50%;
          margin-left: -5px;
        }
      }
    }
  .cloned-expand{
      display: block;
      float: left;
      min-height: 80px;
      width: 97%;
      margin: 0 0 10px;
      padding: 10px 30px;
      -webkit-animation: fadeinLoad .5s forwards;
      -moz-animation: fadeinLoad .5s forwards;
      animation: fadeinLoad .5s forwards;
      &-close {
        position: absolute;
        right: -1px;
        top: -1px;
        height: 20px;
        width: 20px;
        line-height: 20px;
        border: 1px solid;
        text-align: center;        
        cursor: pointer;
    }
  }
}
@-webkit-keyframes fadeinLoad {
    from { opacity: 0;}
    to { opacity: 1; }
}
@-moz-keyframes fadeinLoad {
    from { opacity: 0; -moz-transform: translate(0, -20px); }
    to { opacity: 1; -moz-transform: translate(0, 0); }
}
@keyframes fadeinLoad {
    from { opacity: 0; transform: translate(0, -20px); }
    to { opacity: 1; transform: translate(0, 0); }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="grid-list">
  <li class="li">One</li>
  <li class="li">Two</li>
  <li class="li">Three</li>
  <li class="li">Four</li>
  <li class="li">Five</li>
  <li class="li">Six</li>
  <li class="li">Seven</li>
  <li class="li">Eight</li>
  <li class="li">Nine</li>
  <li class="li">Ten</li>
  </ul>

For a runnable example, visit this jsFiddle link

Wishing you success with the resolution :)

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

Guide on: Verifying values using Ajax in PHP and database

Is there a way to send POST values from a form submission and then check if they exist in a MySQL database? Also, what should be included in the corresponding .php file? document.addEventListener("deviceready", onDeviceReady, false); function onDevic ...

Explore the wonders of Google Maps with the convenience of bpopup through ajax

I am currently using the bpopup plugin for ajax popups on my website. I have successfully implemented a popup with a Google map, but I am facing some issues when loading the map asynchronously. JavaScript $(document).on('click', '.aboutBranc ...

Having trouble retrieving all JSON properties

I am facing an issue with my json structure where I am unable to access certain properties. I can only access the main properties like type, properties, and so on within that hierarchy level. However, I cannot seem to access icon, iconURL, or title. The da ...

Creating a dynamic form where input fields and their values update based on user input in jQuery

In my form, I have an input field where users will enter an ISBN number. Based on the input number, I need to populate two other input fields: one for book title and one for author name. I am currently calling a JavaScript function on the onblur event of ...

Verify if the button is assigned a specific class, then generate a 'completed' div

I'm new to working with Jquery and I have a question. In my upload form, when something is uploaded the upload-button changes class from: <a class="upload-button upload buy" id="upload-button"><span>Upload a document</span></a> ...

Fill in according to the options selected in the dropdown menu

Recently delving into the world of React, I encountered a design challenge that needs solving. My goal is to have a selection field with various choice values, each offering different sets of KPIs and UOMs. Depending on the chosen value, I should be able t ...

How to align an unordered list vertically in the center using Bootstrap?

Trying to figure out why the ul within this parent div is not centered vertically. Here is a screenshot of the issue: https://i.sstatic.net/ZOc9M.png <div className=" d-flex align-items-center bg-primary "> <ul ...

Draw rectangles in real-time on a canvas as the mouse is clicked and dragged

Trying to create a drawing program with JavaScript and the HTML canvas. Need help continuously drawing a circle at the mouse location. Code here: <canvas width = '450' height = '450' id = 'drawing'> </canvas> ...

Transferring data between JavaScript and PHP

Is there a way to transfer data from JavaScript to PHP when a button is clicked? For instance, can the getdate() function's result be sent to PHP? This transferred value will then be utilized for database manipulation. ...

Manipulating variables in Jquery is not possible during an ajax success event

I implemented a code feature that uses ajax to load content when a user scrolls to a specific part of the page. To prevent the content from being loaded multiple times, I introduced a boolean variable that should toggle after each ajax call, preventing the ...

Encountered an issue with CSS rendering for PDF files on Gitbook

I was able to use my CSS to customize the design of my Gitbook website. However, I struggled with implementing CSS for the PDF version. When I applied the same CSS used for the website to the PDF, the results were not consistent. It seemed like the CS ...

Begin the animation again by hitting the start button, using the burst-engine and canvas

I have successfully created a simple animation using Burst Engine and HTML5. My goal is to make the start button trigger the animation to restart if pressed twice. Currently, when I press it, the animation just starts and nothing else happens. I suspect t ...

Just started working with React and encountered this initial error message in my project

Welcome to Windows PowerShell! Upgrade to the latest version of PowerShell to enjoy new features and enhancements! PS D:\React> cd textutils PS D:\React\textutils> npm start npm WARN config global `--global`, `--local` are deprecated ...

Customizing Semantic UI Navigation Menu Styles

For the first time, I am incorporating semantic-ui into my React application. The specific package I am working with can be found here: To display my header, I am using the following code: <Menu className='header' > <Containe ...

Transmit ASP Webform data through Visual Studio to an external API

While working on a webform project in Visual Studio, I encountered an issue when trying to send data from the form fields to a 3rd party API using a POST request. Despite my attempts to use JSON to capture the form field data and send it as a JSON object, ...

Adding additional objects to an object overwrites any existing appended data

Check out this pen to see what I'm working on and the issue I'm facing: http://codepen.io/Irish1/pen/lbjdw I've been working on a program that involves adding a week object, where I can input the name of the week along with a description. H ...

Disappearing sticky-top navbar in Bootstrap when scrolling

I'm having trouble with the navbar on my website. Currently, it sticks to the top until I scroll out of the hero-image and then disappears. I would prefer the navbar to remain present throughout the entire page. Any suggestions on how to achieve this? ...

Tips for extracting website language using Selenium in Python

When configuring the language in Selenium, I use: chrome_options.addargument("--lang=en"). During testing, I must verify that the language is indeed set to English. Perhaps I can confirm this by checking the value inside <html lang='... ...

Sorting multiple columns in Angular using the $filter and an array of predicate functions

For my Angular project, I need to order an array using the $filter('orderBy') function. Specifically, I want to specify three predicate callbacks and have the sort order be ASC, DESC, ASC. I am well aware that there are various methods to achiev ...

Is it necessary to clean up the string to ensure it is safe for URLs and filenames?

I am looking for a way to generate a URL-safe filename in JavaScript that matches the one created using PHP. Here is the code snippet I currently have in PHP: <?php $clean_name = strtr($string, 'ŠŽšžŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕ ...