Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

Change Text Color if match found automatically JavaScript|HTML

i have a textarea where i want to change color automatically

for example : this is my pen my friend

as soon as i enter the above text the keyword=pen should become green color and keyword=freind should become red as soon it matches

How do i achieve this thing

code is working but half working

function changeText() 
{
        document.getElementById("text").style.color ="green";
}
</script>

Another Code i have But not Working

 var str = 'Connect'; 
    var value = str.includes('Connect'); 

    if(value==str)
    {
        document.getElementById("text").style.color ="green"; 
    }
    else
    {
        document.getElementById("text").style.color ="red";
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No, you can't do this in a textarea or text input. Any CSS text-related property will affect the whole text within the the textarea/input, not just a word.

see for more info: Multicolor Text Highlighting in a Textarea or Text Input


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...