What is the best way to create a mirror effect on one div by clicking another div?

I have created a schedule grid and I am looking for a way to allow users to click on the UK hour and then have the corresponding U.S time highlighted. Is there a CSS solution for this? The functionality I need is to be able to select multiple hours. I have found some examples but they only work with one div.

https://i.sstatic.net/XlZGm.png

The grid was built using tables, so my code resembles something like this:

<table>
    <tr class="booked">
      <th><a href="">00 am</a></th>
      <th><a href="">01 am</a></th>
      <th><a href="">02 am</a></th>
      <th><a href="">03 am</a></th>
      <th><a href="">04 am</a></th>
      <th><a href="">05 am</a></th>
      <th><a href="">06 am</a></th>
      <th><a href="">07 am</a></th>
      <th><a href="">08 am</a></th>
      <th><a href="">09 am</a></th>
      <th><a href="">10 am</a></th>
      <th><a href="">11 am</a></th>
    </tr>
    <tr class="booked_active">
      <th>4 pm</th>
      <th>5 pm</th>
      <th>6 pm</th>
      <th>7 pm</th>
      <th>8 pm</th>
      <th>9 pm</th>
      <th>10 pm</th>
      <th>11 pm</th>
      <th>12 am</th>
      <th>1 am</th>
      <th>2 am</th>
      <th>3 am</th>
    </tr>
  </table>

Please note: There will be a total of 7 grids, one for each day of the week.

Answer №1

If you need to select individual elements in CSS, you can target one at a time. To target multiple elements, JavaScript is required.

To achieve this with CSS, utilize the :target selector:

The :target pseudo-class selects the unique element that matches an id matching the anchor of the document's URI.

To implement this functionality, assign an id to each element you want to highlight, link it using a hash and the id text, and define a CSS rule for each element:

<th><a href="#cell1">00 am</a>
</th>

<th id="cell1">4 pm</th>

#cell1:target {
  color: red;
}

#cell1:target {
  color: red;
}
#cell2:target {
  color: red;
}
#cell3:target {
  color: red;
}
#cell4:target {
  color: red;
}
#cell5:target {
  color: red;
}
#cell6:target {
  color: red;
}
#cell7:target {
  color: red;
}
#cell8:target {
  color: red;
}
#cell9:target {
  color: red;
}
#cell10:target {
  color: red;
}
#cell11:target {
  color: red;
}
#cell12:target {
  color: red;
}
<table>
  <tr class="booked">
    <th><a href="#cell1">00 am</a>
    </th>
    <th><a href="#cell2">01 am</a>
    </th>
    <th><a href="#cell3">02 am</a>
    </th>
    <th><a href="#cell4">03 am</a>
    </th>
    <th><a href="#cell5">04 am</a>
    </th>
    <th><a href="#cell6">05 am</a>
    </th>
    <th><a href="#cell7">06 am</a>
    </th>
    <th><a href="#cell8">07 am</a>
    </th>
    <th><a href="#cell9">08 am</a>
    </th>
    <th><a href="#cell10">09 am</a>
    </th>
    <th><a href="#cell11">10 am</a>
    </th>
    <th><a href="#cell12">11 am</a>
    </th>
  </tr>
  <tr class="booked_active">
    <th id="cell1">4 pm</th>
    <th id="cell2">5 pm</th>
    <th id="cell3">6 pm</th>
    <th id="cell4">7 pm</th>
    <th id="cell5">8 pm</th>
    <th id="cell6">9 pm</th>
    <th id="cell7">10 pm</th>
    <th id="cell8">11 pm</th>
    <th id="cell9">12 am</th>
    <th id="cell10">1 am</th>
    <th id="cell11">2 am</th>
    <th id="cell12">3 am</th>
  </tr>
</table>

Answer №2

For those utilizing jQuery, achieving this task is quite simple.

Moreover, functionality exists for users to click from the top or bottom as well.

It should be noted that specific jQuery selectors were not styled in this solution. If conflicts arise with other code on the page, selector adjustments may be necessary.

To customize different colors, simply adjust the CSS accordingly.

$('th').on('click',selectTimes)

function selectTimes(){
 $('[hour="'+$(this).attr('hour')+'"]').toggleClass('selected');
}
th {
  height: 20px;
  width: 20px;
}
.booked .selected {
  background-color: yellow;
}
.booked_active .selected {
  background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr class="booked">
    <th hour="1">00 am</th>
    <th hour="2">01 am</th>
    <th hour="3">02 am</th>
    <th hour="4">03 am</th>
    <th hour="5">04 am</th>
    <th hour="6">05 am</th>
    <th hour="7">06 am</th>
    <th  hour="8">07 am</th>
    <th hour="9">08 am</th>
    <th hour="10">09 am</th>
    <th hour="11">10 am</th>
    <th hour="12">11 am</th>
  </tr>
  <tr class="booked_active">
    <th hour="1">4 pm</th>
    <th hour="2">5 pm</th>
    <th hour="3">6 pm</th>
    <th hour="4">7 pm</th>
    <th hour="5">8 pm</th>
    <th hour="6">9 pm</th>
    <th hour="7">10 pm</th>
    <th hour="8">11 pm</th>
    <th hour="9">12 am</th>
    <th hour="10">1 am</th>
    <th hour="11">2 am</th>
    <th hour="12">3 am</th>
  </tr>
</table>

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

Say goodbye to <!DOCTYPE html> when using htmlAgilityPack

I'm currently developing an app for WP 8.1, and one of the tasks I need to accomplish is parsing a webpage with the following structure: <!DOCTYPE html> <html> <body> <table cellspacing="0" cellpadding="0" border="0" style="b ...

What is the best way to generate this arc using CSS3?

What is the most effective method for creating this unique shape using CSS3? The square image should be clearly defined. We greatly appreciate any insight you can offer. EDIT: I have put in a lot of effort, but standard CSS methods are not getting me t ...

Tips for ensuring uniform button height across all cards

I am seeking advice on how to align all the buttons in different cards, despite varying text lengths, to be in the same position at the end of each card. Here is an example of what I am aiming for. Below is my HTML and CSS code: * { margin: 0px; pad ...

Strategies for integrating user data into Vue components within Laravel

I've successfully logged my user data in the console, but when I try to display the data on Contalist page, nothing is returned. I'm new to using Vue and need help implementing it into my projects. Below are my PHP controller and Vue component fi ...

Exporting JSON blend files in Three.js is causing an error that says "Cannot read property 'type' of undefined."

Trying to display the default 3D cube template from Blender v2.74 in Chrome browser, I exported it as json using the threejs v1.4.0 add-on and I'm using Three.js revision 71. Referencing the documentation at , I attempted to load this json model stor ...

Creating a scrollable card layout specifically for small screens in HTML

I am looking to implement a horizontal scroll (for small screens only) within my card layout so that all cards are displayed in a single row. Here is an example: For small screens: https://i.sstatic.net/5eWyV.png So far, I have added horizontal scrolling ...

What is the best way to dynamically showcase options in a React application?

product.js Qty: <select value={qty} onChange={(e) => { setQty(e.target.value) }}> {[...Array(product.countInStock).keys()].map((x) => ( <option key={x + 1} value={x + 1}> ===> I need to dynamically display options b ...

The interaction between Vue components causing changes in each other's data

Currently, I am working on a project using vue/nuxt. In order to dynamically load data from a JSON file during compilation, I am utilizing nuxt and webpack (Dynamically get image paths in folder with Nuxt). The structure of my JSON file is as follows: { ...

Experimenting with an rxjs service to perform a GET request within the component

When calling the service function, the syntax is as follows: get getLayerToEdit(): BehaviorSubject<VectorLayer> { return this.layerToEdit; } This function is then invoked in the ngOnInit method like this: ngOnInit() { this.annoServic ...

The right-aligned navigation bar elegantly shifts downward when it exceeds the screen width

I've been struggling to create a Right Aligned Navigation Bar with a search bar and a login icon that stay in one row on the page. No matter how I try, it keeps jumping down instead of aligning properly. Here is an image of what I'm trying to ach ...

What is the best way to manage the browser tab close event specifically in Angular, without it affecting a refresh?

I am trying to delete user cookies when the browser tab is closed. Is this achievable? Can I capture the browser tab close event without affecting refreshing? If I attempt to use beforeunload or unload events, the function gets triggered when the user ref ...

Place the two buttons in position

I have a section where I need to include two buttons. One button on the left labeled "Sunday" and one on the right labeled "misc". I want the two buttons to be evenly placed within the area, so I assigned them the same class since they are the same size. ...

Discovering the most recently selected element in jQuery

Currently reading a jQuery tutorial from the Head First series and practicing with an example where I need to identify the last selected element. In Chapter 2, there is a task involving four images on a page. When a user clicks on any of these images, the ...

"Troubleshooting the non-functional :before pseudo-element in a Select-Box

I am having trouble with my CSS. Below is the code I am using: /*Custom Select Box*/ select#user_select { background-color: var(--white); float: right; color: var(--dark_grey); width: 30%; height: 3rem; padding-left: 3%; bord ...

The issue with jquery slideToggle is that it mistakenly opens all divs instead of just the specific div that should

My website has a dynamic page with a variable number of <div> elements. The concept is that users can click on the + symbol, which is represented by an <img> tag, to display the corresponding div. Currently, my code includes: PHP/HTML $plus ...

Disable the 'bouncy scrolling' feature for mobile devices

Is there a way to prevent the bouncy scrolling behavior on mobile devices? For example, when there is no content below to scroll, but you can still scroll up and then it bounces back when released. Here is my HTML structure: <html> <body> ...

Is it possible to bind to a service variable in a controller without using $scope and utilizing the

As I delve into the world of controllerAs syntax in AngularJS, I've encountered a snag when attempting to bind a service variable. The traditional approach using `$scope.$watch` or `$scope.$on` would require injecting `$scope`, which seems counterintu ...

Encountering a NPM error when trying to launch the server using ng serve

I encountered an error : ERROR in /opt/NodeJS/FutureDMS/src/app/app.module.ts (5,9): Module '"/opt/NodeJS/FutureDMS/src/app/app.routing"' has no exported member 'APP_ROUTE'. Within my code, I have utilized arrow function in the loadCh ...

What is the reason for the successful insertion into the database without including "http://" but encountering errors when including "http

How can I insert a site title, site address (URL), site description, and site category into my database using jQuery/JSON on the front-end? When I enter "http://" in the site address input field, the data is not inserted into the database. Here is the cod ...

Firestore data displaying as null values

Recently, I encountered CORS errors while polling the weather every 30 seconds in my program. Upon investigating, I discovered that the city and country were being interpreted as undefined. To fetch user data from my users' table, I utilize an Axios ...