Targeting Select box with JQuery

By: Ryan Wong at

I always forget how to target select box that has been selected when I need it most.

So I decided to make a blog post so I can get the answer really fast.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//find out selected value
var type = $('#type :selected').val();

//get changed value
$('#type').on('change', function(e){
var value = this.value;

//dynamically making select box
$.getJSON("/api", {
//data
}).done(function(a) {
$.each(a, function(a, b) {
$("#select").append($("<option/>", {
value: b.value,
text: b.text
}))
});
});

Hope it helps.