Creating a dynamic content space: A step-by-step guide

Project Overview

I'm currently working on a cinema demo website, where I have implemented four movie poster panels displaying the poster image, title, and screening times. These panels are meant to be clickable.

My goal is to have an interactive feature where clicking on any panel will update the content below with text and a trailer video specific to the selected movie. The other three areas should remain hidden when one panel is clicked.

Although I have successfully designed the panels and container area, I am struggling to implement the functionality that replaces the content upon clicking.

Progress So Far/Code Implementation

I have researched similar queries like this one, but none of them fully address my current issue.

Your assistance in solving this dilemma would be greatly appreciated!

Below is the code for the movie panels:

/* Panels */
.panel {
  border: 1px solid #333;
  border-radius: 15px;
  width: 70%;
  margin: 0 auto;
  margin-bottom: 2rem;
}

.panel h3 {
  font-size: 1.2rem;
}

/* Additional CSS styling goes here... */



I have explored further options and found this example which aligns closely with what I am trying to achieve. I attempted to integrate it into my code, but it did not produce the desired outcome.

Script.js file:

$("#movie-one").click(function() {
  let synopsis = $(this).attr(".synopsis");
  $("div").html(synopsis);
});

Answer №1

If you have static content on your page, you can leverage javascript to handle interactions.

Here's a simplified version with a script that toggles CSS classes to show and hide movie details:

let selectedMovie = undefined;

// Function to handle movie selection
let createMovieSelectionHandler = function(movieId) {
    return function() {
        // Hide previously selected movie
        if (selectedMovie) {
            document.getElementById(selectedMovie).classList.remove('selected');
        }

        // Show selected movie
        selectedMovie = movieId;
        document.getElementById(selectedMovie).classList.add('selected');
    };
};

// Find all movie elements on the page
let movies = document.getElementsByClassName('movie');

// Add event listener to each movie element
for (let i = 0; i < movies.length; ++i) {
  movies[i].addEventListener('click', createMovieSelectionHandler(movies[i].dataset.movieId));
}
/* Panels */
.movie {
  border: 1px solid #333;
  border-radius: 15px;
  width: 70%;
  margin: 0 auto;
  margin-bottom: 2rem;
}

.details {
  display: none;
}

.details.selected {
  display: block;
}
<div class="panel-group">
  <div class="movie" data-movie-id="batman">
    <h3>Batman</h3>
  </div>

  <div class="movie" data-movie-id="titanic">
    <h3>Titanic</h3>
  </div>

  <div class="movie" data-movie-id="ghost">
    <h3>Ghost Busters</h3>
  </div>
  
</div>
  <div id="batman" class="details">
    <h3>Batman</h3>
    <p>Lorem ipsum dolor sit amet, conse</p>
  </div>
  <div id="titanic" class="details">
    <h3>Titanic</h3>
    <p>Lorem ipsum dolor sit amet, conse</p>
  </div>
  <div id="ghost" class="details">
    <h3>Ghost Busters</h3>
    <p>Lorem ipsum dolor sit amet, conse</p>
  </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

Utilize the grid system from Bootstrap to style HTML div elements

I am working on my angular application and need help with styling items in a Bootstrap grid based on the number of elements in an array. Here's what I'm looking to achieve: If there are 5 items in the array, I want to display the first two items ...

A step-by-step guide on incorporating the C3 Gauge Chart into an Angular 5 application

I have been attempting to incorporate the C3 Gauge Chart from this link into a new Angular 5 application. Below is my code within chart.component.ts : import { Component, OnInit, AfterViewInit } from '@angular/core'; import * as c3 from &apos ...

Utilize Flexbox to position a group of items in the center of the page, while placing a single item at the bottom for added emphasis

I have utilized Bootstrap 4 along with some custom CSS to create a hero section where all items are centered both horizontally and vertically. The only exception is one item that I want to align at the bottom of the page while still keeping it centered ho ...

What is the best way to determine which CSS class is shown when the page is loaded using JQuery?

I am facing a dilemma with displaying my site logo. I have both an image version and a text version, and I want to choose which one to display based on the vertical position on the page and the screen width. <a class="navbar-brand" id="top-logo" href=" ...

Protected node.js REST API

I want to ensure the security of a restful API and aim to keep it simple and stateless. What is the best way to store, generate, and authenticate API keys? I was considering using node-uuid to generate keys, storing them in Redis, and then authenticating ...

Ways to include a fresh value using an existing key in a document

Is there a way to keep track of multiple categories and their corresponding costs in a category object? For example, if I add the products: $50 category to the user in my code, it displays 'categories': { products: '50$' } in the consol ...

Updating Gmaps iFrame URL with AJAX

I am currently attempting to update the SRC of an iFrame by using a URL obtained via AJAX from the backend database. I want the maps to change when a button is clicked. An error message displayed inside the iFrame reads, "404. That’s an error. The reque ...

Creating an Innovative Grid Design with Varying Column Widths for Each Row

Seeking advice on achieving a specific layout using HTML. I have attempted to use HTML tables but struggled with creating rows with varying columns. Does anyone have recommendations for grid tools that may be more helpful? Thank you ...

Error: 'socket' is inaccessible before it has been initialized, specifically in the context of electron

Trying to configure an electron app where a message is sent to the server, and the server places the value on the read-only textarea. However, upon starting the app, the following error appears in the devtools console: Uncaught ReferenceError: Cannot acc ...

Tips for successfully passing store state as a prop in React-Redux with TypeScript

Having trouble passing information from the initial state of the store to a component where it's supposed to be rendered. Despite a console.log in the component showing that it's undefined, there doesn't seem to be any issue with the initial ...

The issue of Localhost with Functional Components in React.js

I have a page dedicated to showcasing different authors and their details through cards fetched from an API. Each card displays the author's information, and upon clicking it changes to "Remove Favorite" if favorited. The favorited status is toggled b ...

How can you use CSS to style an image's title attribute?

I am trying to customize the text displayed when hovering over an image. Despite my efforts, the code I used doesn't seem to be working properly: <body> <img src="img.jpg" title="<span class='title'>title</span>" &g ...

Having trouble converting HTML to Ajax

When working on my website's Videos Page Layout, I encountered this snippet in the video script: <div class="video-views pull-left"> {$videos[i].viewnumber|kilo} {if $videos[i].viewnumber == '1'}{t c='global.view&apo ...

Issue with QuickSand Script not displaying properly on Internet Explorer 7

My website features an edited version of Quicksand that displays correctly on Chrome, Opera, and Firefox, but runs into CSS issues on IE. Oddly enough, when I was using the static version, the CSS displayed properly even on IE until I added the Quicksand e ...

Tips for adjusting the width of a Facebook embedded video within its container using ReactPlayer

Has anyone encountered issues with adding an embedded video to a NextJS app using ReactPlayer and Facebook videos? It seems that Facebook does not support the width="100%" attribute, as it always snaps back to 500px. Interestingly, this works wel ...

How do I change the 'CSS Theme' of my website?

With Halloween approaching, I am eager to transform my website's BODY css into a spooky Halloween theme. The challenge is that my .Net pages are Templates. Is there a way for me to ensure that the body class checks for an existing CSS theme override? ...

What is the best way to link two Mongoose models using their IDs for cross-referencing?

I need help with a scenario involving two models: project and user. projectSchema = { project_name: String, users: [user_id] } userSchema = { email: String, projects: [project_id] } Imagine a situation where a user wants to create a new project. I ...

Determine the Javascript code needed to identify the button change that was implemented

As a beginner in the world of Javascript, I've been experimenting with incorporating Javascript into my ASP.NET MVC project. Within my project, I have multiple buttons that toggle between different states, such as from ON to OFF. Each button correspo ...

How can the executable path for Firefox be specified in Nightwatch?

I've been using nightwatch to run tests on both Chrome and Firefox with great success. While everything runs smoothly on Chrome, I'm facing an issue when trying to run tests on Firefox on my personal machine. Currently, due to Marionette's ...

Callback function for Jeditable when the form is reset

My situation involves having a string with multiple lines separated by line breaks, which is then parsed into an <ul> hierarchy using JavaScript on the client side. The editing of this list is done using Jeditable, where the user sees the original te ...