Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Length of Div

Tags:

html

jquery

How to find No. of Elements Present in div?? also i want to print list of Element present in div . I have tried this but it is not working ..

<!DOCTYPE html>
<html>
<head>
<title> Div Length</title>
<script type="text/javascript" src="js/jquery-1.6.2.min.js">

</script>
<script>
    $(function ()
    {
    alert(' Elements are ' +$('#main').length);

    }
    )
</script> 
</head> 
<body>

<div id="main">

<img src="images/thumb1.jpg"; height="30" width="30" />
<img src="images/thumb2.jpg"; height="30" width="30"/>
<img src="images/thumb3.jpg"; height="30" width="30"/>
<img src="images/thumb4.jpg"; height="30" width="30"/>
<div id="main1">

</div>

<div id="main2">

</div>


</div>
</body> 
</html>

I need to find length because i am dynamical adding element in div from j query so at the end i want to check what are all elements that has been added successfully .

Thanks,

like image 572
anam Avatar asked Dec 15 '22 11:12

anam


2 Answers

$("div#main").children().length

like image 88
Andrew Klatzke Avatar answered Jan 08 '23 10:01

Andrew Klatzke


If you want to process each item which has been dynamically added you don't need to get the lenght at all. Instead you probably just what to loop though each one like this

$('#main img').each(function(index){
  // Your code
  console.log( $(this).html() );
});
like image 20
Daveo Avatar answered Jan 08 '23 10:01

Daveo