Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This code is used to replace a image on click but I cannot figure how it works

What I have tried:

JavaScript
$(document).ready(function () {
  $(".row a").click(function (e) {
    e.preventDefault();
    $(".imgBox img ").attr("src", $(this).attr("href"));
  });
});</blockquote>
Posted
Updated 29-Mar-22 2:10am
v2

1 solution

You need to look at the jQuery API documentation: jQuery API Documentation[^] Learning to read and use documentation for libraries is incredibly important when it comes to development. You'll need to learn to read and run code to work out how things work, if you expect people to explain things to you any time you get confused or don't understand something then you'll have a really bad time.

That being said, here's a breakdown of the code:
JavaScript
// When the document has finished loading, run function
$(document).ready(function () {

  // Find all links contained within elements with a class of "row"
  // Bind a function to when the link is clicked
  $(".row a").click(function (e) {

    // Prevent any navigation from actually happening from the link click
    e.preventDefault();

    // Find any <img> contained within an element with a class of "imgBox"
    // then update the "src" attribute (the path to the image) with the
    // location of where the link would have sent the user
    $(".imgBox img ").attr("src", $(this).attr("href"));
  });
});
 
Share this answer
 
Comments
OriginalGriff 29-Mar-22 8:21am    
+5!
Himasha Herath (PriVate_xoxo) 29-Mar-22 9:43am    
Thank you so much. I recently started javascript and I'm not familiar with jqeury.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900