The Pencil Guy: Hourann's illogical blog

SELECT objects in the HTML DOM = no fun!

Random JavaScript discovery of the day: suppose you’re working with a <select> drop-down menu in Firefox, and you want to play with the items in the menu.

You might try to use the objects method: document.getElementById("mySelectID").objects … with the expectation that you’d have an array, because that’s what most of the non-official documentation says. But they’re wrong! (or at least out of date?)

What you get pretends it’s an array — you can pick items by index and use the length property — but it’s actually a crazy DOM object called HTMLOptionsCollection. Thus you can’t use funky array techniques like Firefox’s new(ish) indexOf method to search it. That’s a bummer, because JavaScript has no other way (AFAIK) of searching an array by value.

In other news, having coffee with the lawyer-ey portion of the honours crowd after our seminar today was the coolest form of procrastination I’ve done in ages. And yay for Julia’s fifteen minutes (or thereabouts) of exuberant fame :-)

No comments on this post.

Care to leave a comment?