Bhargavi
Bhargavi

Reputation: 851

Div tag events in jquery

What are the div tag events in jQuery ? I need to calculate the length of the div for that i wrote these code .

function drop(ev)
{

 ev.preventDefault();
 var data=ev.dataTransfer.getData("Text");
 ev.target.appendChild(document.getElementById(data).cloneNode(true)); 

     var n = $( "#div2" ).length;
        alert(n);


}

But every time it showing answer as one . when ever i drop more images it must specify the how many images are in div . Will u help me wwhat event i have to write to get this answer .

Upvotes: 2

Views: 108

Answers (4)

Adil
Adil

Reputation: 148140

To find count of images in div you can use descendant selector (“ancestor descendant”)to find descendant of type img in the div having id div2

Live Demo

$("#div2 img").length;

Selects all elements that are descendants of a given ancestor, reference.

Upvotes: 1

Tarun Singhal
Tarun Singhal

Reputation: 1007

To get the length of images img tag inside of div i.e div2 :

var len = $("div2 > img").length;

Upvotes: 0

Alex Art.
Alex Art.

Reputation: 8781

When you use this $( "#div2" ) it means - element with ID="div2". and as you know there could be only 1 such element because id is unique. For getting all the divs use $( "div" ) for getting all the elements wit div2 class use $( ".div2" )

Upvotes: 0

bfavaretto
bfavaretto

Reputation: 71918

when ever i drop more images it must specify the how many images are in div

I believe you're looking for

var n = $( "#div2 img" ).length;

Upvotes: 1

Related Questions