Using jQuery, can we set a portion of a <option>
text to certain color? For example:
<option>John, Rambo, Indiana, Jones, Morpheus, John, McClane, Forrest, Gump</option>
If I want to just change the color of "Morpheus". Is it possible?
Using jQuery, can we set a portion of a <option>
text to certain color? For example:
<option>John, Rambo, Indiana, Jones, Morpheus, John, McClane, Forrest, Gump</option>
If I want to just change the color of "Morpheus". Is it possible?
If you meant option value of a select
box, there is no easy way or even cross-browser solution. You will have to resort to some custom solution using LIs
to mimic a customized select box and then you can style it with jquery.
For example, check out the one option here that you can customize (add colors, etc) however you like.
I don't see jquery helping you much here, but some plain javascript can do this:
"John,Rambo,Indiana,Jones,Morpheus,John,McClane,Forrest,Gump".replace(/Morpheus/gi, "<p style='color:blue'>Morpheus</p>")
If you'd like to change the color (using JQuery) while the application is running, you can try something like...
<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");
Try this:
$("#mySelect option:contains('Morpheus')").css("background-color","red")