Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery add class to object with .get() method

running into a weird thing and I'm not sure what's going on.

I've grabbed the index of a DOM element via .index(), found a matching element via .get() and I'm trying to add a class to it via .addClass().

My console is returning the error: "Uncaught TypeError: Object #<HTMLLIElement> has no method 'addClass'"... which is especially odd because my Log shows the HTML element just fine (http://cloud.dhut.ch/image/2W3S0R3k2h2U)

Am I missing something? It's not returning in an array or anything. Confused.

Thanks!

JavaScript:

nFLi.get(active).addClass('active');

like image 917
technopeasant Avatar asked Sep 11 '12 04:09

technopeasant


People also ask

How do I add a class to a jQuery element?

jQuery addClass() Method jQuery HTML/CSS Methods. Example. ... The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.

What is addClass () method in jQuery?

The addClass () method is an inbuilt method in jQuery UI framework which is used to manage user interface visual effects. This method adds class to all selected elements along with animating all the defined styles in CSS properties.

What does the add () method do in jQuery?

The add () method adds elements to an existing group of elements. This method adds elements on the whole document, or just inside context elements if the context parameter is specified. Required. Specifies a selector expression, a jQuery object, one or more elements or an HTML snippet to be added to an existing group of elements

How to create object oriented classes in jQuery?

jQuery – create object oriented classes in jQuery Published by Joe Buckleon September 23, 2015 Create an object oriented style with jQuery. Make use of the constructor() method and access public and private methods from within the class scope.


1 Answers

You need to wrap it into a jquery object.

$(nFLi.get(active)).addClass('active');

Or you could use .eq method instead of .get, which returns a jquery object instead of original HTMLElement.

nFLi.eq(active).addClass('active');
like image 191
xdazz Avatar answered Sep 30 '22 03:09

xdazz