A_J
A_J

Reputation: 71

How to check div has image or not?

<div id="pl6" style="height: 123px; width: 135px;float:left" onmousedown="test(6);" ondrop="dropIt(event,6);" ondragover="event.preventDefault();">
<img src="6.gif" draggable="true" ondragstart="dragIt(event,6);" id="pic6" />
</div>

I want to check if this div has an image or not using JavaScript.

Upvotes: 1

Views: 809

Answers (3)

mfq
mfq

Reputation: 1377

if(document.getElementById("pic6").length > 0){
  //...
}

Upvotes: 1

Krasimir
Krasimir

Reputation: 13529

Why not simply:

if(document.querySelectorAll("#pl6 img").length > 0) {
   // ...
}

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

you could do:

var divEle = document.getElementById("pl6");
if( divEle.getElementsByTagName('img').length > 0 ) {
   //has image element
}

Upvotes: 5

Related Questions