Is there a more efficient approach to applying a basic function to multiple elements within a DIV and having it activate only upon a click event using jQuery?

This solution works, but I am looking for a more professional approach in order to adhere to the principle of "don't repeat yourself."

$("document").ready(function(){
    $(".child").each(function(index){
        $(this).click(function(){
            $(".hire-me").eq(index).fadeIn("slow");
        });
    });
});

I would appreciate a shorter implementation method.

Answer №1

If you want to achieve this functionality, you can utilize the index() method to find out the index of the current .child element. Subsequently, you can utilize this dynamic value in your calls to the eq() method.

$("document").ready(function() {
  $(".child").click(e => {
    let index = $(e.target).index();
    $(".hire-me").eq(index).fadeIn("slow");
  });
});
.hire-me { display: none; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="child-container">  <a href="#" class="child">Child #1</a>
  <a href="#" class="child">Child #2</a>
  <a href="#" class="child">Child #3</a>
  <a href="#" class="child">Child #4</a>
  <a href="#" class="child">Child #5</a>
  <a href="#" class="child">Child #6</a>
</div>

<div class="hire-me-container">
  <div class="hire-me">Hire Me #1</div>
  <div class="hire-me">Hire Me #2</div>
  <div class="hire-me">Hire Me #3</div>
  <div class="hire-me">Hire Me #4</div>
  <div class="hire-me">Hire Me #5</div>
  <div class="hire-me">Hire Me #6</div>
</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

Having trouble adjusting the right-hand edges of form elements when using percentage widths

Simply put, the issue is this: Why doesn't the second row of elements, with widths of 35% and 55%, take up the same amount of the page's width as the first form element which uses 90%? I often have to adjust the percentages differently dependin ...

Is there a way to display JSON data within my AJAX success callback function?

I'm encountering an issue where the JSON data returned from the server is not accessible in my success callback function within an AJAX request. $.ajax({ url:'myData.php', type:'post', success:function(data){ c ...

Break the line after every space in words within an element

I have a table/grid view with two words in the <td> like "C2 Sunday". I need a line break after C2 so that it appears as: C2 Sunday Is there a way to achieve this? Please assist me with this issue. Currently, it is showing as "C2 Sunday" and I wou ...

Employing the html.validationmessagefor to display a client-side error notification

My text fields have server-side validation messages. The field 'title' is a required field on the server side with a '[Required]' data attribute in the class, but it only seems to be checked on a postback or form submit. I am saving the ...

Struggling to align the footer of your webpage and ensure it adjusts proportionally with the window size?

After trying several codes that have worked before, I ran into a problem when attempting to place two div side by side for the footer. No matter what code I tried, I couldn't get it centered and responsive to the user window ratio. Even after consulti ...

My observations on CSS challenges in IE7/8 and the Hashchange plugin

What is causing these float issues in Internet Explorer? Additionally, has anyone had any experience with 'jquery.ba-hashchange.min.js' and know how to make the hashchange function more visually appealing? Thank you! ...

The problem of border-radius bleeding in Webkit when applying -webkit-transform

After creating a basic jQuery script to slowly rotate an image, I encountered an issue where the rotated image would bleed over the border-radius in Chrome. The rotation works smoothly in Firefox 4.0.1 but not in Chrome. To see the example and code, check ...

Enlarging the image size to fill the width of its container

Is there a way to combine the stretching or compressing of several images into one size, specifically 175 by 250 pixels? Here's how it can be achieved: <input type = "image" src = "@ Url.Content(topRated.Value)" onclick = " ...

Issue with image preview functionality in dialog modal when loading content via ajax request

<table id="result"> <a href='#' class='pop'><span class='fa fa-eye'><img src='image link here' style='display:none'></a> </table> The hyperlink above is designed to open ...

VS code is showing the directory listing instead of serving the HTML file

Recently, I attempted to use nodejs to serve the Disp.html file by following a code snippet from a tutorial I found on YouTube. const http = require("http"); const fs = require("fs"); const fileContent = fs.readFileSync("Disp.html& ...

Refresh the pivot table based on the selection of checkboxes

My form consists of checkboxes with lists of holiday names as values. The scenario involves a many-to-many relationship where One ZONE can have One/Many HOLIDAY and One HOLIDAY can belong to One/Many ZONE. To manage this relationship, we use a pivot tabl ...

Adding custom fonts to DOMPDF: a step-by-step guide

First, I moved the dompdf library folder to /dompdf Next, I added a /fonts folder containing Roboto-Black.ttf Then, I created an index.php file <?php // Autoloader inclusion require_once 'dompdf/autoload.inc.php'; // Namespace reference ...

Steps for adding an onclick function to a collection of div elements

My current project involves utilizing the Flickr API and I am interested in creating a popup div that opens when clicking on each image. The goal is to display the image in a larger form, similar to how it's done using Fancybox. I'm looking for ...

Tips on resizing images in CSS with accompanying text to prevent overlap

In my school project, I am using cloud9. When utilizing the live preview in a small window pane, my images appear correctly sized. However, upon maximizing my screen browser, the images stack on top of each other and their dimensions increase. My ideal l ...

Tips for aligning two divs vertically and horizontally next to each other

I'm struggling to center two divs both horizontally and vertically at the top of my page. I've tried various methods like "vertically align" & "margin: auto 0", but nothing seems to work. Can anyone help me figure out what I'm doing wron ...

The total sum of various divs containing a mix of numbers and letters

I have a group of div elements with the same class, each containing a value in the format (X1), (X2),... I need to add up these numeric values only, like 1 + 2 + .... Since these divs are generated dynamically, I won't know how many there will be. How ...

Can you explain the distinction between align-content and align-items?

Can you explain the distinction between align-items and align-content? ...

How can I utilize a PHP variable as the height and width parameters in a CSS style?

After spending a considerable amount of time on this project, I am still struggling to make it work. Can someone please guide me on how to correctly assign the width and height values to the img element using variables? for ($x = 1; $x <= $aantal; $x+ ...

Tips on aligning an entire text in R Markdown using the bookdown::gitbook function as the desired output

I've been looking for a way to justify the text in my gitbook, but haven't had any luck finding a solution. I've attempted: Adding "text-align: justified" to the CSS right after the YAML Header: <style> body { text-align: justify ...

After loading the ajax content, remember to include the JavaScript files

Here's the situation: I am importing some php files, one of which includes a slider that requires .js files. However, when I make an ajax call, the file is imported but the js files are not. Is this normal behavior? I attempted the following: var s ...