How to get the ID of an element inside a div?

0

I have a div and some images, each image represents an item in a global array . When I drag this image to% editing% an editing form is enabled, otherwise a form is enabled to create new rule and generate a new image.

I need to know when there is an image in div to enable inclusion or editing mode and for this I need to know the ID of it since it is the array index.

if(document.getElementById("manage").innerHTML == ""){
    console.log("vazio");
else{
    var id = document.getElementById("manage").innerHTML;
    console.log(id.attr("id"));
}

As "id" is a string with the content:

img id="ico-3" class="img-drag" src="images/isp.jpg" draggable="true" ondragstart="drag(event)"

In this example of IMG, the index in the array is 3.

    
asked by anonymous 05.02.2015 / 17:45

1 answer

4

Considering that you only have one image within that div:

// Usando JavaScript puro
var img = document.querySelector("div.manage img.img-drag");
var id = null;
// Se a imagem existe na div
if(img) id = img.id;

// Usando jQuery
var id = $("div.manage img.img-drag").attr("id");

If the image exists id will be the id string, otherwise it will be null (first example) or undefined (jQuery).

    
05.02.2015 / 18:47