All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

jQuery Change Image Src

Last Updated : Mar 11, 2024

jQuery Change Image Src

In this article we will show you the solution of jQuery change image src, a fresh picture can be chosen using the jQuery alter image src approach if you wish to dynamically update the image on your page.

If you are worked with HTML a little bit previously and are familiar with the tag and how to set the src property to point to a file location of a picture you would like to load.

Now move to the concept of jquery change image src.

Step By Step Guide On jQuery Change Image Src :-

Using JQuery and JavaScript, the aim is to modify the src attribute of the <img> element.

jQuery prop() Method:

The jQuery prop() method sets and returns the elements' attributes and values when a match is made.

The first selected element's value is returned if the property value is returned using this method.

When setting property values, this technique sets one or even more property/value pairs for the collection of selected items.

Syntax:

$(selector).prop(property)
<!DOCTYPE HTML>
<html>
 <head>
  <title>
  jquery change image src
  </title>
  <script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
  </script>
 </head>
 <body style = "text-align:center;">
  <h1 style = "color:green;" >
   TalkersCode
  </h1>
  <p id = "TK_UP" style =
   "font-size: 15px; font-weight: bold;">
  </p>
  <img id = "img" src =
"http://talkerscode.com/images/logo.png" />
  <br>
  <button onclick = "TK_Fun()">
   Click me for Change
  </button>
  <p id = "TK_DOWN" style =
   "color:green; font-size: 20px; font-weight: bold;">
  </p>
  <script>
   $("#TK_UP").text("Click on button to change the src of image using jquery...");
   $('button').on('click', function(e) {
    $('#img').prop('src',
'https://talkerstech.com/images/logo.png');
    $("#TK_DOWN").text("You can see that our image src attribute is changes");
   });
  </script>
 </body>
</html>
  1. With the aid of HTML and HEAD tags, we begin our coding. Using the TITLE tag, we then add a title to our website.
  2. The jquery library is then imported using script.
  3. After that, we use the BODY tag to begin the body of our code. To utilise centre as an alignment in the body, use the style tag.
  4. Using an H1 tag and a green background, we then add the heading to the webpage.
  5. After that, we utilise an id for the P tag, which provides style attributes like font weight and size.
  6. After that, we utilise image src with id.
  7. After that, a button with the onclick function set to its value is created.
  8. Next, we utilise a different id for the P tag, which likewise contains a style with a font's colour, weight, and size.
  9. We then begin our script. With the use of the P tag, we created various ids that we then used in the script to write some content for each id.
  10. Next, with the help of the onclick function, we use that button.
  11. Next, we use the jQuery prop() method to modify our image src.
  12. There is also another method which is The jQuery on() function adds one or so more event handlers for such chosen components and their children.
  13. Then, for the P tag, we add some additional text to the second id.
  14. Finally, using the HTML, SCRIPT, and BODY tags, we put an end to our code.

Conclusion :-

Consequently, we were able to understand the idea of jQuery changing an image's source, we also discovered that.

If you have any experience working with HTML, you should be able to set its src attribute of a tag to point to the location of a file containing a photo you want to load.

I hope this article on jQuery change image src helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪