Javascript - How can I check if an element contains an image?
html, javascript
Solution
You can get the image elemant by using js getElementById then you can easily access the src of that image, which tells you weather element contains image if src is not empty or if contains then src should have image path.
<html>
<head>
<script>
function myFunction()
{
var imgSrc = document.getElementById("myImg").attr('src');
alert(imgSrc);
}
</script>
</head>
<body>
<h1>My Web Page</h1>
<p id="demo">A Paragraph</p>
<img id="myImg" src="http://www.blah.com/img.jpg" onclick="myFunction()"></img>
</body>
</html>
Problem
Can anyone suggest how I could check if an element contains an image? I'm assuming it would be whether or not it contains the image source but am unsure how to code this.