Monday, February 13, 2012

How to count elements and take attribut of first?


I want to count all img elements and take attribute (alt) of FIRST of it. My try:




$('img').each(function(index) {
/* need count and take forst */
if(index=0){
var objid = $(this).attr('alt');
}
});



Can you help me with this?

2 comments:

  1. var $images = $('img');
    var imgCount = $images.length;
    var altAttrOfFirst = $images.first().attr('alt');

    ReplyDelete
  2. var $img= $('img');
    var count = $img.length;
    var attr = $img.first().attr('alt');

    ReplyDelete