Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addClass to element inside an array

can you help me understand why this is not working?

var elementTab1 = $('#tab1 .item-media.modificato');
elementTab1[0].addClass('selezionato');

this through this error

TypeError: undefined is not a function (evaluating 'elementTab1[0].addClass('selezionato')')

Thanks

like image 542
elledienne Avatar asked Dec 23 '14 22:12

elledienne


1 Answers

elementTab1 is already a jQuery object. It contains an array of matched elements in the DOM. Accessing the first index using [0] will return a native element with access to the native JavaScript API (and not jQuery's).

jQuery does provide a nice way to grab items from the array though. It is .eq().

elementTab1.eq(0).addClass('selezionato');
like image 67
Travis J Avatar answered Sep 21 '22 18:09

Travis J