So I'm building a slider control from a dropdown list.
The jQuery UI example uses javascript that looks like this:
<script>
$(function() {
var select = $( "#minbeds" );
var slider = $( "<div id='slider'></div>" ).insertAfter( select ).slider({
min: 1,
max: 6,
range: "min",
value: select[ 0 ].selectedIndex + 1,
slide: function( event, ui ) {
select[ 0 ].selectedIndex = ui.value - 1;
}
});
$( "#minbeds" ).change(function() {
slider.slider( "value", this.selectedIndex + 1 );
});
});
</script>
I'm basing my work on that, but trying to make it more dynamic - I'm writing a function to turn select elements into sliders. I can guarantee that the select elements have options in sorted order, but it isn't clear to me how I can take the var $select ($ sign added for distinction as a container of a jQuery object) and use it with other jQuery selectors.
For example, how do I use a variable holding a jQuery object to find the first child of it? I know something like the following will work:
var selected = $('#select-element option:selected').val();
$('#foo').slider({ value: selected });
It isn't clear to me, however, how I can take the variable $select and get the option:selected off of it (or option:first-child, options:last-child, etc).
In short, I am wondering how I mix typical jQuery selectors with variables that hold jQuery objects.
.find()$('#select-element option:selected')you can do$('#select-element').find('option:selected')so if you have a variable for select element then$select.find('option:selected')