1

Javascript:

var ulKopjes = document.getElementsByClassName("tabKopjes")[0].getElementsByTagName("li");
 for (j=1; j < (ulKopjes.length - 1); j++){
  debugger;
   if (ulKopjes[j].hasClass('activeTab')){
    debugger;
   }
 }

Is there an obvious reason that I get the error 'Uncaught TypeError: undefined is not a function'?
I checked the values in the array, those ar 'li' items, that's correct.

3
  • ulKopjes[j] is a dom element, so you have to wrap in a $() Commented Dec 9, 2014 at 10:03
  • You also seem to have a &#60; char in your code Commented Dec 9, 2014 at 10:04
  • My code is a part of XSLT. XSLT doesn't accept < in javascript. Commented Dec 9, 2014 at 10:09

2 Answers 2

1

try

$('.tabKopjes').eq(0).find('li').each(function() {
   if ($(this).hasClass('activeTab')) {
       //do stuff
   }
});

or if the li are direct children of .tabKopjes use .children instead of .find

If you are just after the li with the class active tab you can do

$('.tabKopjes').eq(0).find('li.activeTab')
Sign up to request clarification or add additional context in comments.

2 Comments

@OP This is the way it should be written using jQuery, utilize it if it's already there.
You're welcome! eq(0) is the first item in the found list like your [0] on the array. api.jquery.com/eq
0

hasClass is not a pure-native-JS method and it does not exist. It's a jQuery function, so you have to use jQuery to get your elements, for example:

var ulKopjes = $('.tabKopjes:first > li');

If you do not use jQuery, you have to use another way to check for a given class: Test if an element contains a class?

3 Comments

Can I also use $(document.getElementsByClassName("tabKopjes")[0].getElementsByTagName("li")); ?
Actually yes, you can, but why? The jQuery syntax is so much shorter.
I changed it to var ulKopjes = $('.tabKopjes > li');, this didn't work. Do I have to change my if statement too?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.