最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - Change event.target src attr in JavaScript - Stack Overflow

matteradmin7PV0评论

On clicking image I want to change its src attribute using event.target.

I am trying to achieve the same using below code but it is not working; where am I going wrong?

$(".sim-row-edit-img").click(function(){
	alert("Image clicked");
	$("event.target").attr("src",".png");
});
<script src=".1.1/jquery.min.js"></script>
<img class="sim-row-edit-img" border="0" alt="W3Schools" src=".png" width="100" height="100">

On clicking image I want to change its src attribute using event.target.

I am trying to achieve the same using below code but it is not working; where am I going wrong?

$(".sim-row-edit-img").click(function(){
	alert("Image clicked");
	$("event.target").attr("src","https://cdn.iconscout./icon/free/png-256/car-location-find-navigate-gps-location-29571.png");
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img class="sim-row-edit-img" border="0" alt="W3Schools" src="http://icons.iconarchive./icons/dtafalonso/android-l/256/WhatsApp-icon.png" width="100" height="100">

Share Improve this question edited Feb 11, 2020 at 8:27 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Sep 11, 2018 at 6:30 user2828442user2828442 2,5257 gold badges67 silver badges120 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

event.target is an object , so remove the quotes from event.target

$(".sim-row-edit-img").click(function() {
  alert("Image clicked");
  $(event.target).attr("src", "https://cdn.iconscout./icon/free/png-256/car-location-find-navigate-gps-location-29571.png");
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img class="sim-row-edit-img" border="0" alt="W3Schools" src="http://icons.iconarchive./icons/dtafalonso/android-l/256/WhatsApp-icon.png" width="100" height="100">

You should use $(this).attr() as $('event.target') will not give you a valid jQuery object. Using $(this) will give you the reference of the clicked element and then you can change the src attribute of that element.

$(".sim-row-edit-img").click(function() {
  alert("Image clicked");
  $(this).attr("src", "https://cdn.iconscout./icon/free/png-256/car-location-find-navigate-gps-location-29571.png");
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img class="sim-row-edit-img" border="0" alt="W3Schools" src="http://icons.iconarchive./icons/dtafalonso/android-l/256/WhatsApp-icon.png" width="100" height="100">

To use event.target you need to pass the event object to the function and no need to use $(event.target), simply use event.target.src = 'value'.

$(".sim-row-edit-img").click(function(event){
	alert("Image clicked");
	event.target.src = "https://cdn.iconscout./icon/free/png-256/car-location-find-navigate-gps-location-29571.png";
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<img class="sim-row-edit-img" border="0" alt="W3Schools" src="http://icons.iconarchive./icons/dtafalonso/android-l/256/WhatsApp-icon.png" width="100" height="100">

Post a comment

comment list (0)

  1. No comments so far