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 .
html:
js: