Retrieve the text content of the paragraph element when its parent is clicked. The paragraph element belongs to a group that contains many

In a function I'm working on, I need to retrieve the specific text within a p element when its parent (.photoBackground) is clicked. The purpose of this operation is to grab the caption for a gallery, where there are multiple p elements with the same class. My goal is to extract the text from the clicked element and then copy it to an empty .photoCaptionBig element. Currently, I have a click function in place for other functionalities:

$('.photoBackground').on('click',function ()
. How can I achieve this?

Here is the HTML code snippet:

<div id="lightbox_caption_container">
    <a id="lightbox"></a>
    <div class="photoCaptionEffect">
        <p class="photoCaptionBig" style=""></p>
    </div>
</div>

<div class="itemContainer">
    <div class="photoBackground" id="photo1">
        <div class="photoHoverEffect">
            <p class="photoCaption" style="">TextTextText</p>
        </div>
    </div>
</div>

and here is the corresponding CSS code snippet:

.photoBackground {
    transition: box-shadow 0.3s ease;   
}
.photoBackground:hover{
    box-shadow: 0 0 0.938em rgba( 0, 0, 0, .7 );
    transition: box-shadow 0.3s ease;
}
.photoBackground:hover .photoHoverEffect {
    transition: opacity 0.4s ease;
    opacity: 1;
}
.photoHoverEffect, .photoCaptionEffect {
    height: 65px;
    width: 100%;
    margin-top: 155px;
    background-color: rgba(18,18,18,0.6);
    display: -webkit-inline-box;
    opacity: 0;
    transition: opacity 0.4s ease;
}
.photoCaptionEffect {
    height: 85px;
    opacity: 1;
    position: absolute;
    z-index: 1333;
    margin-top: 365px;  
}
.photoCaption, .photoCaptionBig {
    font-family: raleway;
    font-size: 13px;
    color: #fff;
    margin-top: 8px;
    margin-left: 8px;
    line-height: 16px;
    display: -webkit-inline-box;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
}

Thank you!

  • Magnus Pilegaard

Answer №1

When using jQuery, you can handle click events on objects like this:

$('selector').click(function() {
    // Finding a <p> element within the clicked object
    var $p = $(this).find('p');
    // Checking if the <p> has a specific class
    var $p = $(this).find('p.particularClass');
    // Getting the text inside the <p>
    var $text = $p.text();
});

Cheers.

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

Encountered an issue when executing python manage.py migrate and python manage.py runserver

form.py This is the form section. from django.contrib.auth.models import User from security_app.models import UserProfileInfo from django import forms class UserForm(forms.ModelForm): password = forms.CharField(widget=forms.PasswordInput()) ...

Customize stepper background color in Angular Material based on specific conditions

I am working on a project using Angular Material and I want to dynamically change the color of the stepper based on different states. For example: state: OK (should be green) state: REFUSED (should be red) Here is what I have done so far: <mat-horizo ...

Jquery vertical menu with a dynamic sliding animation from the right side to the left side

Hello everyone, I am looking to have my vertical menu slide from right to left instead of the typical top to bottom sliding that comes with using .toggle() in jQuery. Specifically, when the hamburger menu is clicked, I want the menu to slide out from right ...

Using dynamic variables in the $.getJSON function

This specific inquiry represents my current goal, with an added layer of complexity. My aim is to streamline the process by creating a single 'fetchData' function in my VueJS project that can retrieve multiple JSON files without duplicating code ...

JavaScript - Executing the change event multiple times

Below is the table I am working with: <table class="table invoice-items-table"> <thead> <tr> <th>Item</th> <th>Quantity</th> <th>Price</th> & ...

Altering the flex-direction causes my divs to vanish into thin air

Just starting out with css, trying to get some practice. I'm attempting to position two divs on opposite sides of a parent div. Switching flex-direction from 'column' to 'row' causes the divs to disappear. #assignments-wrapp ...

Tips for identifying the amount of <ul> elements contained within every div

Dealing with a large file structured in the same way, I am looking for a method to isolate a specific div, determine the number of ul's within it, and then iterate through each one to extract the value of every li element. <div class="experiment ...

Tips on making a dropdown select menu expand in a downward direction

I'm currently using a select element to choose options from a dropdown list, but because of the large number of items, the list displays in an upward direction instead of downwards. I am working with Bootstrap. I have reviewed other code samples with ...

Retrieve information from a PHP file using AJAX when the output is just a single line

I never thought I would find myself in this situation, but here I am, stuck. I just need a single result from this PHP file, so is using an array really necessary? Despite my efforts to console.log(result) multiple times, all I get back is "null". What c ...

Transferring data from AJAX to PHP class methods

Q. Is it feasible to transfer data from ajax to a specific php class with functions? For instance, verifying a username on the registration form to check if the user already exists. This form is straightforward and will gather a username input along with ...

What steps do I need to follow in order to create an AJAX application that functions similarly to node

As someone new to ajax, I am facing challenges while trying to create a forum software similar to nodebb. Despite having developed a php forum previously, its outdated appearance prompted me to seek alternatives like nodebb for a more modern look and feel. ...

Managing AJAX Errors in PHPAJAX error handling tips for developers

My current code is only set up to display a general error message when an error occurs during execution. I want to improve it by returning specific error messages for different scenarios. However, I have not been able to find satisfactory solutions in my s ...

Tips for retaining the value of a variable when the page is reloaded

I need to store the value of the variable loggedIn, as it determines the behavior of a function in appComponent.html. Can you explain how I can achieve this? Template of app component: <li class="nav-item"> <a class ...

Tomcat: jamming out to some tunes

My web application includes a feature to play audio files uploaded to the server. I'm using the following code for this: <object id="MediaPlayer" type="application/x-oleobject" height="42" standby="Installing Windows Media Player..." width="138" 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 ...

Toggle the hamburger menu using JavaScript

How can I close my hamburger menu when clicking a link for one page navigation? The menu is functioning properly, but I need a way to close it. Unfortunately, I have limited knowledge of JS. I only have the HTML and CSS for this: HTML in index.html file ...

In Redux, it is possible to add characters to an array but for some reason the remove action does not successfully reach the reducer

As a user types or erases characters, I am utilizing redux to update an array of characters. This allows me to set a success flag when the user correctly types the entire phrase. Currently, when typing in characters, the SET_INPUT action in redux fires of ...

Transform bootstrap CHECKBOX into the appearance of a bootstrap BADGE

Is there a way to create Bootstrap checkboxes that resemble bootstrap badges, while still functioning as checkboxes? I attempted to style the checkbox label as a badge, but it was unsuccessful. <head> <link rel='stylesheet' href= ...

Tips for generating an input element using JavaScript without the need for it to have the ":valid" attribute for styling with CSS

My simple input in HTML/CSS works perfectly, but when I tried to automate the process by writing a JavaScript function to create multiple inputs, I encountered an issue. The input created with JavaScript is already considered valid (from a CSS ":valid" sta ...

CSS DROP DOWN NAVIGATION MENU - Struggling to maintain hover effect on main menu item while navigating through sub-menu

I am facing a challenge with a CSS-only drop-down menu where the top main menu item loses its highlight when I move the cursor over the sub-menu items. You can view the menu in action here: . For example, if you hover over "Kids" and then move your cursor ...