Is there a reason you're running a $.each for the selected option? It
seems there's only one possible selected option at once.

        $("select").change(function() {
                $selected = $(this).find("option:selected");
                $("#result").css( 'background-color', $selected.css
("background-color") );
                $(".foo").text( $selected.text() );
        });


On Apr 19, 8:30 pm, "geh...@googlemail.com" <geh...@googlemail.com>
wrote:
> hello everybody,
>
> i'm new in jquery (and javascript).
> ive try to write a little function to change the background-color for
> an element.
> it can change the text but not the background-color.
> any help?
>
> -----------jQuery---------------
>         $("select").change(function() {
>                 var bar = "";
>                 var foo = "";
>                 $("select option:selected").each(function() {
>                         bar += $(this).text();
>                         foo += $(this).css("background-color");
>                 });
>                 $("#result").css('background-color', foo);
>                 $(".foo").text(bar);
>         }).change();
>
> -------- HTML -------------------
> <select name="id[1]">
>         <option style="background-color:#ff0000;">red</option>
>         <option style="background-color:#00ff00;">green</option>
>         <option style="background-color:#0000ff;">blue</option>
>     </select>
>
> <div id="result">element with new background-color</div>

Reply via email to