Using jQuery to change the color of a piece of text in an option element

Using jQuery, is it possible to set a piece of text <option>for a specific color? For instance:

<option>John, Rambo, Indiana, Jones, Morpheus, John, McClane, Forrest, Gump</option>

If I want to just change the color of Morpheus. Is it possible?

+3
source share
4 answers

If you had in mind the value of the option select, there is no simple or even cross-browser solution . You will need to resort to some special solution with LIs, to emulate a custom checkbox, and then you can create it using jquery.

For example, check here for one option that you can customize (add colors, etc.) as you like.

+4

, jquery , - javascript :

"John,Rambo,Indiana,Jones,Morpheus,John,McClane,Forrest,Gump".replace(/Morpheus/gi, "<p style='color:blue'>Morpheus</p>")
+2

( JQuery) , - ...

<SELECT>
<OPTION>What is your preferred browser?</OPTION>
<OPTION id="b1" style="color:Blue">Explorer 5.5</OPTION>
<OPTION id="r1" style="color:Red">Explorer 6.0</OPTION>
</SELECT>

Then ... use jQuery to change the CSS color for the "b1" or "r1" element ...

$("#b1").css("color", "#FF0000");
0
source

Try the following:

$("#mySelect option:contains('Morpheus')").css("background-color","red")
0
source

Source: https://habr.com/ru/post/1788986/


All Articles