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

On clicking image I want to change its src attribute using event.target.I am trying to achieve the same

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">

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745614199a4636131.html

相关推荐

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

    On clicking image I want to change its src attribute using event.target.I am trying to achieve the same

    6小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信