When selecting an option, the color of the selected option is marked

Asked

Viewed 56 times

3

I have the following select that is coming from the database as per the image below:

inserir a descrição da imagem aqui

<?php
...
while($listar = mysqli_fetch_object($sql)){

   $mostrar .= " <select name='Situacao[]' class='md-form-control'>
                  <option value=''>Selecione</option>
                  <option value='Pago' style='background-color: #006400; color: #FFF'>Pago</option>
                  <option value='Não Pago' style='background-color: #F00; color: #FFF'>Não Pago</option>
                  <option value='Aguardando' style='background-color: #FF8C00; color: #FFF'>Aguardando</option>
               </select>";    
}
...
?>

And the options are:

inserir a descrição da imagem aqui

It is possible that when selecting one of the options:

inserir a descrição da imagem aqui

The select has the color as selected option?

  • Hello Isac, yes... but when I do this, select is not the color of the option. For example: if I select Pago, I would like the select to be green, but it is still white.

  • Sorry Isac. I altered my post. I really ended up not being clear in my doubt.

  • 1

    As I saw php, I thought it was related to the value, only after I realized that the goal was the style.

2 answers

3


You can get the style of the selected option with attr for the attribute style and applies it to select. To get the selected you can use find(":selected").

Example:

$("select.md-form-control").on("change", function(){
  $(this).attr("style", $(this).find(":selected").attr("style"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name='Situacao[]' class='md-form-control'>
    <option value='' style=''>Selecione</option>
    <option value='Pago' style='background-color: #006400; color: #FFF'>Pago</option>
    <option value='Não Pago' style='background-color: #F00; color: #FFF'>Não Pago</option>
    <option value='Aguardando' style='background-color: #FF8C00; color: #FFF'>Aguardando</option>
 </select>

Note that I had to include a style='' in the option Selecione to return to the initial style.


You can also do the same with classes that are more flexible and scalable. In this case not only is the application slightly different, but the html of the <select> also looks different.

Before applying a new class remove the ones you already have with removeClass passing only the ones we want to remove. Then get the new one to apply from the selected element with attr('class').

Example:

$("select.md-form-control").on("change", function(){
  $(this).removeClass("pago naopago aguardando"); //remover qualquer uma destas classes
  $(this).addClass($(this).find(":selected").attr('class'));
});
.pago {
  background-color: #006400; 
  color: #FFF;
}

.naopago {
  background-color: #F00; 
  color: #FFF;
}

.aguardando {
  background-color: #FF8C00; 
  color: #FFF;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name='Situacao[]' class='md-form-control'>
    <option value='' >Selecione</option>
    <option value='Pago' class='pago'>Pago</option>
    <option value='Não Pago' class='naopago'>Não Pago</option>
    <option value='Aguardando' class='aguardando'>Aguardando</option>
 </select>

  • Perfect Isac. It worked! I used the first option. Thank you so much!

2

The @Isac response is very good, but I would like to leave another suggestion for reference, where it is not necessary to put style='' at first option as he suggested.

With cssText you can rewrite the style, and if you don’t find it in option (in the case, the first), it will be empty returning the select to initial state.

Behold:

$("select.md-form-control").on("change", function(){
   this
   .style
   .cssText = $(":selected", this).attr("style");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name='Situacao[]' class='md-form-control'>
   <option value=''>Selecione</option>
   <option value='Pago' style='background-color: #006400; color: #FFF'>Pago</option>
   <option value='Não Pago' style='background-color: #F00; color: #FFF'>Não Pago</option>
   <option value='Aguardando' style='background-color: #FF8C00; color: #FFF'>Aguardando</option>
</select>

  • 1

    Hello dvd. Thanks for the tip.

  • 1

    It is indeed an interesting alternative

  • Obg @Isac......

Browser other questions tagged

You are not signed in. Login or sign up in order to post.