-3

I need to write a function to check the number of options in a selector tag using javascript. The only thing is I cannot use jQuery (My script is based on puppeteer and web scraping). If you have any suggestions please leave them down below. Example:

enter image description here

I need the function to return 2 in this example.

Thanks.

Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
Zach C
  • 71
  • 1
  • 10

2 Answers2

0

For this you can use the following code snippet:

var x = document.getElementById("size").length;

x will hold the number of options here.

source: https://www.w3schools.com/JSREF/prop_select_length.asp

  • This is because you are using puppeteer in node.js , the code given is the equivalent in scripted javascript To see how you would use a similar function(not the exact same as the one you require) in puppeteer see this: https://stackoverflow.com/questions/52256799/how-to-use-document-getelementbyid-in-nodejs – Griffinhand Jun 01 '20 at 13:38
0

You can use children property to extract option from select

document.getElementById('size').children you will get HTMLCollection which consists of all the option present inside that select

To get the count document.getElementById('size').children.length

You cannot iterate over HTMLCollection to perform an action
You need to convert the HTMLCollection to Array in order to perform an action
Array.from(document.getElementById('cars').children)

Aditya toke
  • 461
  • 4
  • 14