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")
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")
3 Answers
Reset to default 12var allSelects = document.getElementsByTagName("select");
var lastSelect = allSelects[allSelects.length-1];
A more elegant solution:
document.querySelector("select:last-child")
You shouldn't be using more than one element with the same Id. A better option is document.getElementsByTagName
i. e. document.getElementsByTagName("input")[document.getElementsByTagName("input").length - 1]
mylist
... – brso05 Commented Nov 13, 2015 at 15:43myList
. Use e.g. class instead Document.getElementsByClassName(). – t.niese Commented Nov 13, 2015 at 15:43