-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.

1
  • document.querySelectorAll('select#size > option').length but it is better to use the form id element Commented Jun 1, 2020 at 13:06

2 Answers 2

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

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

1 Comment

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: stackoverflow.com/questions/52256799/…
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)

3 Comments

@ZachC have you fixed the document error ? from the link provided by Mamun
yes I will look into it after I have finished my school work
@ZachC ok do let me know if you still got any issue

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.