I'd suggest, in the absence of any HTML in the question:
// binds the change to a select element (_ALL_ select elements):
$("select").change(function () {
// assigns the value of the selected option to the 'color' variable
var color = $(this).val()
/* changes the CSS of the '#selectBox' element, to set the color
and updates the text of that element to reflect the chosen option/color:
*/
$('#selectBox').css('color', color).text(color);
// triggers the change event, to trigger the change-handler on page-load/DOMready
}).change();
JS Fiddle demo.
Coupled with the following HTML:
<select name="color" id="color">
<option value="red">red</option>
<option value="blue">blue</option>
<option value="green">green</option>
</select>
<div id="selectBox"></div>
Note that, if you have multiple select
elements on the page, you must use a more specific selector to bind the correct event-handling (unless all the select
elements are there to update the same CSS of the same element).
References: