How to Change the Image Source Using jQuery
attr()
You can use the attr() method to change the image source (i.e. the src attribute of the <img> tag) in jQuery. The following example will change the image src when you clicks on the image.
src
<img>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery Change Image Source on Click</title> <style> .card{ margin: 30px; } </style> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script> $(document).ready(function(){ $("img").click(function(){ // Change src attribute of image $(this).attr("src", "images/card-front.jpg"); }); }); </script> </head> <body> <div class="card"> <img src="images/card-back.jpg" alt="Poker Card"> </div> </body> </html>
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Use the jQuery
attr()MethodYou can use the
need an explanation for this answer? contact us directly to get an explanation for this answerattr()method to change the image source (i.e. thesrcattribute of the<img>tag) in jQuery. The following example will change the imagesrcwhen you clicks on the image.