How to select the last one of a certain element in JavaScript

Ned picture Ned · Nov 13, 2015 · Viewed 7.6k times · Source

I have several <select> elements in my page. Is there any easy way to select the last one of them? This brings me the first one:

document.getElementById("myList")

UPDATE:

Sorry for the wrong usage of getElementById. Let me change my question: How to access the last one of a certain element using getElementsByTagName?

document.getElementsByTagName("select")

Answer

Ned picture Ned · Nov 13, 2015
var allSelects = document.getElementsByTagName("select");
var lastSelect = allSelects[allSelects.length-1];