Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i access 'src' attribute of <img> tag in javascript?

Tags:

javascript

<html>
<head></head>
<body>
<div id="ctl00_ContentPlaceHolder1_ctl00_ctl01_Showcase">
<div style="width:100%;text-align:center;"><img src="http://www.xyz.com/aaa.gif" id="ctl00_ContentPlaceHolder1_ctl00_ctl01_loderImg" alt="Loading" /></div>        </div>
<script>
q= document.getElementById('ctl00_ContentPlaceHolder1_ctl00_ctl01_Showcase').childNodes[1].getAttribute('src').innerHTML;

alert(q);

</script>
</body>
</html>

how do i access 'src' attribute of img tag ? im above code it gives null value so whats wrong ?

like image 994
Pooja Desai Avatar asked Jun 25 '12 04:06

Pooja Desai


Video Answer


2 Answers

You can use src on your image directly. Also, you don't need .innerHTML.

Demo: http://jsfiddle.net/ThinkingStiff/aU2H2/

document.getElementById( 'ctl00_ContentPlaceHolder1_ctl00_ctl01_loderImg' ).src;

HTML:

<html>
<head></head>
<body>
<div id="ctl00_ContentPlaceHolder1_ctl00_ctl01_Showcase">
    <div style="width:100%;text-align:center;"><img src="http://placekitten.com/100/100" id="ctl00_ContentPlaceHolder1_ctl00_ctl01_loderImg" alt="Loading" /></div>        
</div>
<script>
    q = document.getElementById( 'ctl00_ContentPlaceHolder1_ctl00_ctl01_loderImg' ).src;
    alert(q);
</script>
</body>
</html>
like image 193
ThinkingStiff Avatar answered Nov 04 '22 11:11

ThinkingStiff


alert(document.getElementById('your_image_id').getAttribute('src'));
like image 24
coolguy Avatar answered Nov 04 '22 11:11

coolguy