Tips for connecting elements at the same index

.buttons,
.weChangeColor {
  width: 50%;
  height: 100%;
  float: left;
}

.weChangeColor p {
  background: red;
  border: 1px solid;
}

.toggleColor {
  background: green;
}
<div class="buttons">
  <p><a href="#">FirstLink</a></p>
  <p><a href="#">SecondLink</a></p>
  <p><a href="#">ThirdLink</a></p>
</div>

<div class="weChangeColor">
  <p>FirstPara</p>
  <p>SecondPara</p>
  <p>ThirdPara</p>
</div>

By clicking on the links above, I want each corresponding paragraph to change its background color to green dynamically using JavaScript.

I am looking for the JavaScript code that will achieve this functionality.

If possible, jQuery can also be used to accomplish this task.

Answer №1

To achieve this functionality, you can utilize the jQuery index() and eq() methods.

Below is a demonstration:

$(".buttons p").click(function(){
   $(".weChangeColor p").eq($(this).index()).toggleClass("toggleColor");
   $(this).toggleClass("toggleColor");
});
.buttons,
.weChangeColor {
  width: 50%;
  height: 100%;
  float: left;
}

.weChangeColor p {
  background: red;
  border: 1px solid;
}

p.toggleColor {
  background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="buttons">
  <p><a href="#">FirstLink</a></p>
  <p><a href="#">SecondLink</a></p>
  <p><a href="#">ThirdLink</a></p>
</div>

<div class="weChangeColor">
  <p>FirstPara</p>
  <p>SecondPara</p>
  <p>ThirdPara</p>
</div>

Answer №2

If you are experiencing issues, please review the following code snippet:

Thank you

jQuery('.buttons p').click(function(){
  var ClickedElemenet = jQuery(this).index();
  var GetElement = jQuery('.weChangeColor p').get(ClickedElemenet);
  jQuery(GetElement).toggleClass('toggleColor');
});
.buttons,
.weChangeColor {
  width: 50%;
  height: 100%;
  float: left;
}

.weChangeColor p {
  background: red;
  border: 1px solid;
}

.toggleColor {
  background: green !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="buttons">
  <p><a href="#">FirstLink</a></p>
  <p><a href="#">SecondLink</a></p>
  <p><a href="#">ThirdLink</a></p>
</div>

<div class="weChangeColor">
  <p>FirstPara</p>
  <p>SecondPara</p>
  <p>ThirdPara</p>
</div>

Answer №3

To achieve a pure CSS solution, you can utilize the pseudo-class :target to target p elements by assigning an id to each of them.

Here's an example:

.buttons,
.weChangeColor {
  width: 50%;
  height: 100%;
  float: left;
}

:target {
  background: green;
}
<div class="buttons">
  <p><a href="#p1">FirstLink</a></p>
  <p><a href="#p2">SecondLink</a></p>
  <p><a href="#p3">ThirdLink</a></p>
</div>

<div class="weChangeColor">
  <p id="p1">FirstPara</p>
  <p id="p2">SecondPara</p>
  <p id="p3">ThirdPara</p>
</div>

Answer №4

One way to implement this functionality in JavaScript using jQuery is as follows:

jQuery('.buttons p').click(function(){
  jQuery('.weChangeColor p').eq($(this).index()).toggleClass('toggleColor');
});
.buttons,
.weChangeColor {
  width: 50%;
  height: 100%;
  float: left;
}

.weChangeColor p {
  background: red;
  border: 1px solid;
}

.weChangeColor p.toggleColor {
  background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="buttons">
  <p><a href="#">FirstLink</a></p>
  <p><a href="#">SecondLink</a></p>
  <p><a href="#">ThirdLink</a></p>
</div>

<div class="weChangeColor">
  <p>FirstPara</p>
  <p>SecondPara</p>
  <p>ThirdPara</p>
</div>

Alternatively, you can achieve the same result by utilizing classes, IDs, or data attributes.

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

Tips for defining a dynamic class variable in React with Flow

I am working with a map that assigns a reference to items, specifically in this scenario it is a video. const ref = this[`video-${index}-ref`]; I am seeking guidance on how to properly type this using Flow. The number of indexes may vary. ...

Distinguishing between native and custom error objects: a comprehensive guide

When working with errors in my node app, I find it challenging to handle both custom and native errors seamlessly. Errors are not just ordinary JavaScript objects, which adds complexity to error handling. To manage custom errors, I am experimenting with t ...

Alteration of Content Height Input

I have designed a layout that I am excited to experiment with more in the future. Here is the current structure of my HTML: <!DOCTYPE html> <html> <head> <title>Title</title> <link rel="stylesheet" type="text/css" hre ...

align items center with respect to my central element

Describing my issue might be a bit complex, but I'll give it a shot. I'm working on a website and have 3 div elements in the header for my navigation bar (burger menu / logo / social networks). I've used flex display with justify-content: be ...

Angular form displayed on the screen

I'm having trouble finding a solution to this issue, as the form data is not being output. var app = angular.module('myApp', []); app.controller('mainController', ['$scope', function($scope) { $scope.update = funct ...

Arranging Elements Using CSS

Currently, I am working on aligning and positioning my items using CSS. However, I'm a bit confused about the right approach. Here is a demo of my current progress: Demo Link I aim to achieve a layout similar to this sketch: Sketch Image Apologies ...

What is the best way to make a hyperlink in HTML open in the same page or window, without relying on the <iframe> tag or JavaScript?

I have two html files and one css file. My question is how to open the link monday.html on the same page or window in my content section. For example, when someone clicks on the Monday navigation button, I want the result to show up in the content section ...

In Google Chrome, the :after element on the left is cleared, while other browsers do not

In an effort to create a button using background images in the :before and :after pseudo-selectors for cross-browser compatibility, an issue arose where only Chrome did not display it correctly (Safari results were unknown). HTML: <div class="btn-cont ...

"Kindly complete all mandatory fields" - The undisclosed field is preventing me from submitting

I am facing an issue with my WordPress page that has Buddyboss installed along with Elementor pro as the Pagebuilder. The Buddyboss plugin provides Facebook-like functions on the website. While it is easy to comment on posts within the Buddy Boss system, I ...

Issue: (SystemJS) XHR error (404) encountered in Angular2 Plnkrsandbox

The issue: https://i.sstatic.net/jUKBU.png https://plnkr.co/edit/910M73kwYKc8xPlSIU57?p=preview index <!DOCTYPE html> <html> <head> <base href="/"> <title>Angular 2.1.2 + TypeScript Starter Kit</title> <met ...

What is the best way to line up three divs horizontally and center the contents inside each one?

I'm brand new to CSS and need some help simplifying things. I am trying to create 3 divs that are all the same size, placed next to each other, with centered content in each one. Currently, I have a central div with a rotating image, and on the left a ...

Tips for eliminating the page margin in Java when converting HTML using iTextPdf with HTMLConverter

No matter how hard I've tried, setting the body with padding: 0 and margin: 0, as well as attempting to set the doc() with setMargin, nothing seems to be effective ...

Increasing numerical values within an array using JavaScript

My goal is to enhance the functionality of this visualization by being able to increase or decrease the nodes in the hidden layers. I have attempted to achieve this by adding the following code: I am facing difficulties in adjusting the number of hidden l ...

Directive in Angular for customer management with filtering and sorting functionality

I am struggling to organize the JSON object by userType using ng-repeat. While I have successfully retrieved and displayed the data, my ISSUE is that I need to sort and display two different tables for the following list: One for MARRIED individuals and an ...

Why does the lazy loading feature keep moving the background image around?

While trying to incorporate lazy loading on my website, I encountered an issue where the image position would change after it was fully loaded and made visible. I experimented with rearranging the order in which my JavaScript and CSS files were called, bu ...

Guide on retrieving information from Spark and showcasing it through Angular

Trying to navigate the world of Spark framework and AngularJS as a beginner, I set out to create a basic REST application. However, I hit a roadblock when it came to retrieving data from the server and displaying it using Angular. My initial task was simp ...

Building interactive web forms with real-time validation using CodeIgniter

I'm looking for a way to use ajax (jquery library) to validate my forms. Specifically, I have a form that requires a minimum password length of 6 characters. Currently, I have implemented the validation rule as follows, $this->form_validation-> ...

Text is not visible when hovering over it

I am facing an issue with the hover effect on my menu element. When I hover over the element, the text does not appear as expected. Even after using z-index to position the text on top, it still doesn't work. Here is a snippet of my code: HTML: < ...

Activate the search feature in select2 to allow multiple selections

I'm looking for a way to incorporate a search box into my multi-select fields using select2. Oddly enough, while the search boxes show up as expected in single-select fields, applying the same select2() function to a multi-select field doesn't s ...

Changing background color during drag and drop in Angular 2: A step-by-step guide

A drag and drop container has been created using Angular 2 typescript. The goal is to alter the background color of the drag & drop container while dragging a file into it. Typescript: @HostListener('dragover', ['$event']) public onDr ...