Reputation: 839
I am trying to get the text "FOUND" by the Reg.Exp. to change color inside of a select menu, but not success, any better way to accomplish this:
<span style="padding-right:40px">
<select id="sel" name="sel">
<option value=""> Choose</option>
<script type="text/javascript">
$( document ).ready(function() {
var myRegExp = /toy/i; /* this text I am trying to change color */
var myText = 'This is a toy, from toys';
if( myRegExp.test(myText) ) {
myText.replace(/toy/ig, '<span class="green">$1</span>')
}
});
</script>
<option value="This is a toy, from toys" id="opt">
This is a toy, from toys
</option>
Thanks for looking!
Upvotes: 0
Views: 68
Reputation: 1716
This is possible with css. No javascript is necessary. You have an ID declared on your option so use that ID to change the color of your text.
#opt { color: green; }
Take a look at this fiddle I made:
Update:
Also, since you have an ID, you do not necessarily need a value specified...which means you could take out the text you have for value="This is a toy, from toys"
as its not needed since you have it already written in between the option tag.
Upvotes: 1
Reputation: 2759
$(document).on("change","#sel",function(){
$(this).find('option').css("color","");
$(this).find('option:selected').css("color","green");
});
Upvotes: 0