Miles
Miles

Reputation: 39

How to obtain reference to all html elements inside a specific class

I would like to obtain an array of all li tags within an element that have a specific class. The problem I seem to get when I run this on my project is it won't give me a reference to all the elements and instead seems to return [prevObject: r.fn.init(1)]. Thanks

const allElements = $('.some-elements li');
console.log(allElements);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<ul class="some-elements">
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
 </ul>

Upvotes: 0

Views: 116

Answers (2)

Alexander Higgins
Alexander Higgins

Reputation: 6923

Try using jQuery instead of $ as some other library may be using $

const allElements = jQuery('.some-elements li');

console.log(allElements);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<ul class="some-elements">
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
 </ul>

Upvotes: 1

JakeBoggs
JakeBoggs

Reputation: 274

You could try:

document.querySelectorAll('.some-elements li');

It will return an array of elements.

Upvotes: 0

Related Questions