`Modify the appearance of the child's image`

I'm currently using this script to create a basic image gallery. However, I am encountering an issue where the border style of the image within 'this' is not changing as expected. Can you help me identify what I might have done incorrectly?

$('.gallery-image').on('click',function(){
    var bigCurrent = $('.hero-image').attr('src'); //the current large image src
    var bigThumb = $('.hero-image').attr('data-smSrc'); //the thumb version src of the current large image
    var thumbSm = $(this).attr('src'); //the currently being clicked on thumb's src
    var thumbLgSrc = $(this).attr('data-lgSrc'); //the large version src of the currently being clicked on thumb

    $('.hero-image').attr('src', thumbLgSrc);
    $('img', this).css('border','2px solid #fff');

});

UPDATE: The code has been updated and now incorporates the corrections suggested in some of the comments below

$('.gallery-image').on('click',function(){
    var bigCurrent = $('.hero-image').attr('src'); //the current large image src
    var bigThumb = $('.hero-image').attr('data-smSrc'); //the thumb version src of the current large image
    var thumbSm = $('img', this).attr('src'); //the currently being clicked on thumb's src
    var thumbLgSrc = $(this).attr('data-lgSrc'); //the large version src of the currently being clicked on thumb

    $('.hero-image').attr('src', thumbLgSrc);

    $('.selected').removeClass('selected');

     $('img', this).addClass('selected'); 
});

Answer №1

If you're encountering issues without witnessing the problem, it's probable that your selection is off base.

The code $(this).attr('src') suggests that this refers to the image node itself since the attribute is specific to img. However, attempting to select an img within this context might be incorrect as an img cannot contain children nodes, making it unlikely to find any nested img elements.

You may want to consider adjusting it to simply $(this).css(...) instead.

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 incorporating a page route with an HTML extension in Next.js

I'm facing a challenge in converting a non-Next.js page to Next.js while maintaining my SEO ranking. To preserve the route structure with HTML extensions and enhance visual appeal, I have outlined the folder structure below: https://i.sstatic.net/zO1 ...

keeping variables hidden from the user until a certain action is taken

In my project, I am working with three main code classes: 1) node.js (utilizing express framework) 2) index.html (serving as the home page when users visit the site) 3) sck.js which performs a specific function (details to follow on the first and second fi ...

Unable to process JavaScript function

Still in the process of developing my "mvc/social" PHP project, I am currently focusing on securing user input for the status message. I have created both a PHP and JavaScript function for this purpose, but it seems like the JavaScript function is not bein ...

Incorporate a JavaScript solution for generating a dropdown menu, rather than relying on

I have been exploring a code snippet that dynamically creates and deletes text boxes using JavaScript. Here is the link to the code in action: http://jsfiddle.net/JpYGg/8/ My goal now is to modify this functionality to generate a set of three drop-down li ...

Is the click count feature malfunctioning?

My goal is to track every mouse click made by the user, so I created a function for this purpose. However, it seems that the function is only counting the first click. To store the count values, I have created a variable. <!DOCTYPE html PUBLIC "-//W3 ...

Update your content dynamically by refreshing it with JQuery/AJAX following the usage of an MVC partial view

Implementing the following JQuery/AJAX function involves calling a partial view when a selection is made in a combobox labeled "ReportedIssue" that resides within the same partial view. The div containing the table is named "tableContent". <script type ...

How can you switch between CSS styles using JQuery?

Is there a way to change CSS properties every time I click using jQuery? I couldn't find any information on this specific topic. Can someone guide me on how to achieve this with jQuery? I want the background color to change each time it is clicked. W ...

Having trouble accessing the value of an item in a dropdown box on a website

I am currently developing a webpage using HTML5 and Knockout Js. I am facing an issue where I am unable to retrieve the ID of the selected item from a dropdown box in order to insert a value into an SQL table. As an example, consider the following SQL tab ...

Struggling with sending form data to the back end when uploading images in Angular

I've been facing a challenge trying to implement profile picture upload alongside standard text data and sending it all to the backend to create a new user through mongoose. Despite my efforts, using tools like ng-file-upload/angular-file-upload and e ...

Sending Location Data From JavaScript to PHP via Cookies

I encountered an issue while attempting to send Geolocation coordinates from JavaScript to PHP using Cookies. The error message I am receiving is: Notice: Undefined index: data in /Applications/XAMPP/xamppfiles/htdocs/samepage.php on line 24 The file name ...

To add additional nested data to a JSON object in JavaScript, you can use the push method or update

Looking to enhance the nested object data within my existing object The current structure of the JSON object array resembles this: var orderDetails = [{ "utilityType": "Electric", "firstName": "ROBERT", "lastName": "GUERRERO", "utilityList": [{ ...

Showing a DIV multiple times with an additional text field in HTML and JS

As someone new to development, I am facing a requirement where I need to create a form with a dynamic field that can be added or removed using buttons. When the user clicks on the Add button, another field should appear, and when they click on Remove, the ...

How to effectively handle asynchronous calls in Node.js using readdir and stat functions

My current project involves implementing a post method on the server side to fetch all files within a specified directory (non-recursively). Below is my code snippet. I am encountering challenges in sending back the response (res.json(pathContent);) with ...

Activate a link, launch a pop-up window, and navigate to the link within the pop-up

I have an unusual idea I'm working on. Let me explain the situation first, and then I'll outline the step-by-step process I want to follow. I currently have a list of items inside a <ul>. Whenever I click on one of these items, I want a mod ...

Unpredictable hues in a headline

Can the text in an H1 tag have each word appear in a different random color, and then refresh to show new random colors? I have 5 specific colors in mind that I'd like to use. How would I go about coding this? ...

Wavy CSS Animation

For assistance with my spinning image, please check out this fiddle: https://jsfiddle.net/dricardo1/9a8p6srb/ Hello! I'm struggling with a spinning image that has a noticeable wobble when rotating. I can't seem to find a solution to make the rot ...

Utilizing JSON Data with Angular

Currently, I am in the process of developing an AngularJS client that communicates with a REST API running on NodeJS. To retrieve data, I make GET requests by accessing specific URLs with stock symbols appended, such as localhost:8080/stocks/aapl for Apple ...

Discover the secrets of accessing two distinct objects returned by a single REST URL with Backbone

I am working with a REST URL that looks like this: /users/<user_id>/entities This URL returns data containing 2 objects as follows: { "players": { "test_player2": { "_id": "test_player2", "user": "f07590 ...

Arrange the array so that the item is placed at the beginning while maintaining the original order

Currently facing a roadblock with this particular issue. I am attempting to create a function that accepts an index as input and rearranges the element at that index in an array to be placed at the beginning, while keeping the order of the other elements a ...

Error encountered: Unable to locate module 'psl'

I'm encountering an issue when trying to execute a pre-existing project. The error message I keep receiving can be viewed in the following error logs image Whenever I attempt to run "npm i", this error arises and I would greatly appreciate it if some ...