I have an array of objects which generated from list of an items from HTML, I faced a problem in dealing with the array to get index of item and the length of the array, always gets Zero.
I want both data-id and value (list 1,2,3 ..).
HTML Code
<ul class="list">
<li data-id="l1">list 1</li>
<li data-id="l2">list 2</li>
<li data-id="l3">list 3</li>
<li data-id="l4">list 4</li>
</ul>
<p></p>
JS Code
var arr = new Array();
$('.list li').each(function(){
var lid = $(this).attr('data-id');
var lval = $(this).html();
arr[lid] = lval;
});
$('p').html('array length = ' + arr.length + ' & index of list 1 is ' + arr.indexOf('l1'));
I'm new in javascript and jQuery i don't know if i have errors with syntax . check out this fiddle please