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

Changing the line spacing of label elements in HTML forms

I am encountering an issue with my form where the spacing between the two lines of the <label> element is too large and I am unable to adjust the line-height. For reference, I have applied the same CSS styles to a <label> and a <p>. Surp ...

Enhance the user experience with a personalized video player interface

I am facing difficulty in creating a responsive video with custom controls. While I understand that using a <figure></figure> element and setting the width to 100% makes the video responsive, I am struggling with making the progress bar also r ...

Dynamic loading of lists necessitates encapsulating nested lists

I'm currently working on a 3-level dropdown list that pulls content from a database for my project. I want the top level to resemble a tab menu, with a border surrounding the area underneath where the additional lists will appear. However, I am facing ...

What is the reason behind both paragraphs being displayed in a shade of blue in this snippet of code

.red p { color: red; } .blue p { color: blue; } <div class="blue"> <p> first </p> <div class="red"> <p> second </p> </div> </div> Initially, I expected the first paragraph to be bl ...

When generating a docx file with html-docx-js, it lacks the capability to incorporate external CSS class styles into the document

I have been utilizing the html-docx-js module to convert html content into docx format. However, I am facing an issue where most of my CSS is externally loaded and html-docx-js does not apply any styles. Here is a simplified code sample: const html = &ap ...

Avoid activating jQuery functions based on certain screen widths for menu/navigation system

Recently, I've delved into the world of jQuery and attempted to create a simple menu system. The menu is designed to expand its submenu when hovering over the list item at screen widths larger than 480px, and to expand when clicking on the list item a ...

Angular JS is having some issues with rendering the style correctly

How can I make input fields turn pink before values are entered and green after values are entered? Currently, the styling only applies to the first row. What could be causing this issue? <script src="https://ajax.googleapis.com/ajax/libs/angularjs ...

Masonry layout organizes images in a vertical stack, with one image per row

After implementing Masonry on my website, I encountered an issue where all the images stack vertically in a single row once the page loads. Instead, I would like them to stack both horizontally and vertically. You can view the problem on My Jsfiddle. This ...

Map Loader for GeoJson Leaflet Integration

Although my English skills are not perfect, I will do my best to explain my issue. I have some knowledge of javascript / html / ajax and I have created a webgis using Leaflet. The problem arises when loading a large geojson file onto the map - it takes qui ...

utilize ajax success method to extract json data

I'm currently working on displaying and parsing JSON data in the success function of an AJAX call. This is what I have so far: AJAX: data = "Title=" + $("#Title").val() + "&geography=" + $("#geography").val(); alert(data); url= "/portal/getRe ...

The Ajax data is appearing in the console, however, it is not being recognized as a valid PHP value

Hey there, I have a question about utilizing Ajax. I've searched for similar issues but can't seem to find anything that matches my problem. Below is the code in question: $(document).ready(function() { $("#nume").blur(function() { numeform = ...

Prevent onClick event in jQuery and extract parameters from a function call

We've been tasked with implementing a temporary solution to some code, so it might seem a bit silly at first. But please bear with us. The goal is to block the onclick method of an anchor tag, extract the parameters from the function call, and then u ...

The clash between Angular's ng-if directive and Bootstrap's popover feature causing unexpected

<div class="form-group" ng-if="firstname"> <label>First Name</label> <input readonly type="text" class="form-control" id="first_name" ng-model="firstname" placeholder="First Name"> <a href="" data-toggle="popover" dat ...

Putting text on top of an image using Bootstrap

Recently diving into the world of Bootstrap, I have come across a puzzling issue: Take a look at the HTML code from my website: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width ...

Tips for resolving HTML issues in AJAX Client Side-templates from WCF-webservice

I am facing an issue with my templates where the returned HTML is encoded by default, and despite my efforts, I can't seem to find a solution. My goal is to substitute NewLines (\u000a) with a simple <br />, but every attempt results in &a ...

How to effectively send data via POST request in ASP.NET to ensure that images are properly displayed on the form

Case Study : An ASP.NET web application includes a page called ShowDesign.aspx The ASPX page is equipped with various controls. Within the page, there's a DIV element where images are dynamically loaded from the code behind. The structure of the DIV ...

Tips for implementing and utilizing onclick functions in EJS

My goal is to develop a trivia game with interactive features. I aim to enable users to click on an answer, which will trigger a border effect and increase the points variable. Below is the layout of the entire page: <% include ../partials/boilerp ...

Leveraging arrays generated from two separate MySQL queries for dual selection functionality with JavaScript

I have successfully populated the first HTML select with results from the first query. Now, I would like to store the results from the second query in either a Json file or XML and then use plain javascript (not jQuery) to populate the second HTML select ...

The Google map is failing to load on the webpage

My id="ieatmaps" is set up to call the googlemaps.js, but for some reason, it's not displaying correctly. I must be missing something. function initMap() { var map = new google.maps.Map(document.getElementById('ieatmaps'), { c ...

Tips to prevent browser from freezing while creating a large number of HTML elements

I am currently utilizing Selection.js to develop a customizable grid on my website. To make this work effectively, I need a specific number of div elements to establish the selectable area. In my scenario, I generate all the divs using a for loop and then ...