I could do something like:
(function( $ ){ $.fn.displayVals = function() { this.change(function() { var val = $(this).val(); var elemid = $(this).attr("data-elem"); if (elemid) $("#" + elemid).html(val); }); }; })( jQuery ); $(function () { $(".display-vals").displayVals(); });
Then, in any selection item you want to work on, you can do something like:
<select class="display-vals" data-elem="val-div"> <option>item 1</option> <option>item 2</option> <option>item 3</option> </select> <div id="val-div"></div>
This uses the html5 attribute "data-". It also means that you donβt need to configure each drop-down menu in the document load event, each of which can specify its own display element in html.
source share