I'm trying to create a new option on the fly and then add it right before the last option of a select.
When I use $("#select").append("<option>hi</option")
, it always adds after the last one.
How do I add a new option before the last option of a select?
$("select option:last").before("<option>hi</option>");
Online demo: http://jsfiddle.net/6MVEd/
$("#select option").eq(-2).after("<option>hi</option")
or
$("#select option").eq(-1).before("<option>hi</option")
"#select option"
is a fully valid querySelectorAll
selector.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With