0

I'm trying to count how many elements (list) have the html data attribute of 'data-facet', but I'm undefined.

Here is what I have tried:

$('li').data('facet').length;

Any ideas where I've gone wrong?

1 Answer 1

2

If data('facet') represents an integer, then that would explain the undefined result (because you can't run .length on an integer). You can fix this by doing $('li').data('facet').toString().length; instead, but that's not going to get you the result you want (instead, it will give you the length of the data-facet value of the first li element).

To get the desired count of lis, do this:

$('li[data-facet]').length

That will give you the number of li element that have the data-facet attribute.

Sign up to request clarification or add additional context in comments.

1 Comment

It was indeed an integer, @Patrick. Thank you for the help :)

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.