0

I have an html select element that I have in a webapp built in Google Apps Scripts. The basic html renders and then asynchronously I add the long list of select items using jQuery and Javascript.

I want to iterate through all of the select options but cannot read more than the original select item that initially renders. In other words, even though I can see my 1300 options on the webpage, the length of the select comes back as one and therefore I cannot iterate.

Here is my initial html:

<select class ="selectNames" id = "studentNameSelect"><option value = "none" selected>Pick your name</option></select>

I then make an html string and add the additional options:

$("#studentNameSelect").append(payload[0]);

This works great for me up to here.

Now when I try to read the length of $("#studentNameSelect") even though I see a long list, length comes as 1.

alert($("#studentNameSelect").length);

What am I missing

6
  • 2
    because you are reading the length of the select element..... not its options Commented Aug 27, 2020 at 17:41
  • 1
    $("#studentNameSelect option").length Commented Aug 27, 2020 at 17:42
  • @RyanWilson, thank you and thanks for going easy on me! Commented Aug 27, 2020 at 17:51
  • @user2420836 No worries. I hope you get the same kind of treatment from others as well. Commented Aug 27, 2020 at 17:52
  • 1300 options is far too many for a user to consume and navigate through. You probably should use an autocomplete type widget instead Commented Aug 27, 2020 at 18:03

1 Answer 1

1

As brought up in the comments on your question, it's because you're selecting the select element instead of the options.

A more resource-friendly way of doing what you want is to store the reference to the select element instead of searching through the document for it twice. On small web pages, it won't make a huge difference, but it can make a difference once your HTML gets complicated. As an example, look at this:

// we store the jQuery representation of the select element in a variable so
// jQuery doesn't have to look through the entire document more than once.
const selectElement = $('#studentNameSelect');

function yourCode() {
  const payload = ["<option>" + Math.round(Math.random() * 1000) + "</option>"];
  
  selectElement.append(payload[0]);

  // now we can search through only the descendants of the select, instead of 
  // making jQuery look through the entire document again to find our new elements.
  alert(selectElement.find('option').length);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>
  <select id="studentNameSelect"></select>
</p>
<p>
  <button onclick="yourCode()">Click me to emulate your code</button>
</p>

We only search the document for the select element once this way.

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

Comments

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.