The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript jQuery text() works on DOM object, but, not addClass()

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • #164339


    ` el = $(this);

    tabs = el.find(“> ul > li > a”);

    alert(tabs.text()); // works

    tabs[0].addClass(“active”) // no method addClass() on object

    tabs.get(0).addClass(“active”) // no method addClass() on object


    what’s wrong with my code? tabs is also a jquery wrapper set, right?


    Are you trying to set the first tab to active? If so you could use: tabs.eq(0).addClass(‘active’);

    eq returns jQuery object at given index.
    get returns DOM element which doesn’t have method addClass.


    When you select something with jQuery, jQuery puts it in an array. so el is an array, and tabs is an array. And you can perform jQuery things on this array.

    When you use something like tabs[0] or tabs.get(0) you’re selecting an element of the array in a normal javascript and NON-jQuery way. This is why you can’t use addClass() because its no longer a jQuery object.

    So how do we select an element in a jQuery way? There’s a couple ways. You can do something like $(tabs[0]) or $(tabs.get(0)) which will re-select the new javascript element with jQuery.

    Or you can just use the .eq() property.

    Try tabs.eq(0) This is like get() which you used, but it keeps the selected element in jQuery.

    I hope this helps.


    Got it.. :)

    Thank you ryanknights and Noahgelman ….

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.