admin管理员组文章数量:1024704
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
2 Answers
Reset to default 3event.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">
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
2 Answers
Reset to default 3event.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">
本文标签: jqueryChange eventtarget src attr in JavaScriptStack Overflow
版权声明:本文标题:jquery - Change event.target src attr in JavaScript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745614197a2159186.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论