How can I get all the options of a select through jQuery by passing on its ID?
I am only looking to get their values, not the text.
How can I get all the options of a select through jQuery by passing on its ID?
I am only looking to get their values, not the text.
$.map
is probably the most efficient way to do this.You can add change options to
$('#selectBox option:selected')
if you only want the ones that are selected.The first line selects all of the checkboxes and puts their jQuery element into a variable. We then use the
.map
function of jQuery to apply a function to each of the elements of that variable; all we are doing is returning the value of each element as that is all we care about. Because we are returning them inside of the map function it actually builds an array of the values just as requested.You can take all your "selected values" by the name of the checkboxes and present them in a sting separated by ",".
A nice way to do this is to use jQuery's $.map():
You can use following code for that:
This will put the option values of
#myselectbox
into a nice clean array for you:Use:
.each() | jQuery API Documentation
This is a simple Script with jQuery: