144
rated 0 times
[
148]
[
4]
/ answers: 1 / hits: 48172
/ 12 Years ago, sun, april 22, 2012, 12:00:00
I need to get the Image source when onclick event of the image is fired .
I tried using document.getElementById(example).src; , but the console on browser says
Uncaught TypeError: Cannot read property 'src' of null
<html>
<head>
<script type=text/javascript>
function changeIt()
{
var name = document.getElementById(myimage).src;
alert(name);
}
</script>
</head>
<body>
<img onclick=changeIt() src='em1.gif' name='example' border='0' />
<img onclick=changeIt() src='em2.gif' name='example' border='0' />
<img onclick=changeIt() src='em3.gif' name='example' border='0' />
<img onclick=changeIt() src='em4.gif' name='example' border='0' />
<img onclick=changeIt() src='em5.gif' name='example' border='0' />
</body>
</html>
Update part .
The actual source is , on a AJAX Response , i will get Image Paths from a folder , for which i need to create Images dynamically and add them to a div .
This is actual code
outputdata.forEach(function (element) {
content = $(<div><a href='something'><img onclick='changeIt()' src=' + element + ' /></a></div>);
content.addClass('content');
container.append(content);
$(#fp_thumbScroller).append(container);
});
}
so i didn't created ID , thinking as it is not suitable to have same id's for different images .
More From » javascript