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 the value of the img tag or its id? 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
Add a ment  | 

3 Answers 3

Reset to default 2

Try 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