Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  148] [ 4]  / answers: 1 / hits: 48173  / 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

 Answers
300

html:



<img onclick=changeIt(this.src) src='em1.gif' name='example' border='0' />


js:



function changeIt(_src){
alert(_src);
}​

[#86066] Saturday, April 21, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sandra

Total Points: 708
Total Questions: 100
Total Answers: 84

Location: Bosnia and Herzegovina
Member since Thu, Jun 24, 2021
3 Years ago
sandra questions
Tue, Jun 30, 20, 00:00, 4 Years ago
Sun, May 31, 20, 00:00, 4 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Fri, May 31, 19, 00:00, 5 Years ago
;