admin管理员组文章数量:1404923
How can i get value of img tag??? in img tag we have one property named "Value" so i want to know that how can i access this value property in javascript??? I use this code:
function addcart()
{
$cartvalue=confirm("Would You like to add This Product to Your Cart?");
if($cartvalue)
{
var product=document.getElementById('prod1').value;
alert(product);
}
}
here prod1 is value of img tag but it display as undefine in alert
How can i get value of img tag??? in img tag we have one property named "Value" so i want to know that how can i access this value property in javascript??? I use this code:
function addcart()
{
$cartvalue=confirm("Would You like to add This Product to Your Cart?");
if($cartvalue)
{
var product=document.getElementById('prod1').value;
alert(product);
}
}
here prod1 is value of img tag but it display as undefine in alert
Share Improve this question asked Jan 16, 2013 at 13:48 Adarsh BhattAdarsh Bhatt 11 gold badge1 silver badge1 bronze badge 3- 1 Can you show us the HTML markup? – Anthony Forloney Commented Jan 16, 2013 at 13:49
-
2
prod1
is thevalue
of the img tag or itsid
? Post your HTML please – Michael Berkowski Commented Jan 16, 2013 at 13:49 -
<img>
s don't have values. – Quentin Commented Nov 15, 2022 at 10:11
3 Answers
Reset to default 2Try something like
var product=document.getElementById('prod1').getAttribute('value');
Um.. I could be way off here, but arn't you looking for the src
attribute? I don't think that value
is a valid attribute for the <img>
tag...
var product=document.getElementById('prod1').src;
As I see it, the "value" of an image would be the path to the image itself.. That is located in the src
attribute.
AFAIK the img-tag normally has no attribute value: http://www.w3schools./tags/tag_img.asp
But the solution of Michael should work.
Alternatively you can try it with jQuery:
var product=$('#prod1').attr('value');
本文标签: javascriptHow to get Value of img tagStack Overflow
版权声明:本文标题:javascript - How to get Value of img tag? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744878512a2630061.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论