$("#vremenno img").eq(i).attr("id") is undefined

FireBag displays this error.

Here is my code:

 for (i = 0; i < arrayPodkid.length; i++) { $('#vremenno').prepend(arrayPodkid[i]); } for (i = 0; i < arrayPodkid.length; i++) { if (!$('#vremenno').eq(i).attr('src')) { if (resultPodkid > parseInt($('#vremenno img').eq(i).attr('id').match(regV), 10) / 10) { resultPodkid = Math.floor(parseInt($('#vremenno img ').eq(i).attr('id').match(regV), 10) / 10); var m = parseInt($('#vremenno img ').eq(i).attr('id').match(regV), 10); } 

In CTML is the following

  <div id="vremenno"> <img id="103" height="260" width="174" src="images/рубашка.jpg"> <img id="102" height="260" width="174" src="images/рубашка.jpg"> </div> 
  • regV as global variable set - Radik Kamalov
  • and in arrayPodkid what lies? + Are you sure that the images are not less than the size of the array? - Sh4dow
  • arrayPodkid array with pictures. This is exactly what I check - Radik Kamalov
  • At will, it works, 1 time out of 10 it comes out. I don’t know how to track down, I’m sitting with a debagger for a very long time, because I don’t know if there is a mistake now or not - Radik Kamalov
  • Firstly, a record of this type - $('#vremenno').eq(i) - suggests that you have several div elements with the same identifier! This is already a blunder. Secondly, lay out logically complete code snippets in order to avoid unnecessary questions and get rid of telepathy sessions. - Deonis

1 answer 1

I'll suggest this refactoring:

 for(var i in arrayPodkid) $('#vremenno').prepend( arrayPodkid[i] ); for(var i in arrayPodkid) { var img = $('#vremenno img').eq(i); if(!img.attr('src')) { if (resultPodkid > parseInt( img.attr('id').match( regV ), 10) / 10 ) { resultPodkid = Math.floor( parseInt( img.attr('id').match(regV), 10) / 10 ); var m = parseInt( img.attr('id').match( regV ), 10); } } } 

@Radik Kamalov , do not disdain spaces and indents) Strongly increase the readability of the code. In the min-version will compress, if necessary.

By the way, thanks @Deonis :) " $('#vremenno').eq(i) " is also a mistake, I tried to fix it logically.

Offtopic: no matter how hard I tried, I did not understand what this code should do. Removed obvious errors and optimized.