views:

40

answers:

4

img src tag

i want, when click on image the source of the image in javascript variable i want to use the src in javascript

A: 
var imageSource = document.getElementById ( "yourimageid").src;

A sample one

<script type="text/javascript">
    function GetSrc(elem)
    {
        alert ( elem.src );
    }

</script>

<img src="images/yourimage.extn" id="img1" onclick="GetSrc(this);" />
rahul
A: 
<img src="image.png" id="image" alt="image">
<script type="text/javascript">
    alert(document.getElementById('image').src);
</script>

This will alert the src of the element with the id 'image'

Tim
A: 

Also you can use document.getElementById("[elementName]").getAttribute("src") and setAttribute..

Gennadich
A: 

rahul answer works

udhaya