Solved

certain text in string inside of TextArea is hioghlighted

Posted on 2014-01-05
5
226 Views
Last Modified: 2014-03-04
I am looking for a solution, when a certain word is written it would change color or be bold
so for example:

"Golf requires a different skillset than basketball."

After the user types in the word Golf it would change to green.  After the user types in the word basketball, it turns yellow and is bolded.  No button needs to be pressed....probably runs on keypress event
0
Comment
Question by:GlobaLevel
5 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
you CAN'T change color of text inside a textarea
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
Comment Utility
What if you could simply add the keywords above the text box.

<div id="keywords">basketball,baseball</div>
<textarea>I got a basketball jones... Baseball, has been very very good to me.</textarea>

Open in new window

0
 
LVL 24

Accepted Solution

by:
mankowitz earned 500 total points
Comment Utility
You can colorize a contenteditable. For example:

html:
<p id="asd" contenteditable>I like big horses and a cat Text</p>

Open in new window


js:
$(function() {
    
    var wordcolors = {
        "horses": "blue", 
        "cat": "pink",
        "nostril": "#af41e9"
    };
    
    function colorify() {
        $("#asd").html($("#asd").text().replace(/\b\w*\b/g, function(x) {           
            return (x in wordcolors) ? "<span style='color: " + wordcolors[x] + "'>" + x + "</span>" : x;
            }));
        
        setEndOfContenteditable($("#asd")[0]);              
    }
                      
                                                                                   
    
    $("#asd").keyup(colorify);
    
    
    
});

function setEndOfContenteditable(contentEditableElement)
//thanks to stackoverflow.com/questions/1125292/how-to-move-cursor-to-end-of-contenteditable-entity for the cursor positioning
{
    var range,selection;
    if(document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
    {
        range = document.createRange();//Create a range (a range is a like the selection but invisible)
        range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range
        range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
        selection = window.getSelection();//get the selection object (allows you to change selection)
        selection.removeAllRanges();//remove any selections already made
        selection.addRange(range);//make the range you have just created the visible selection
    }
    else if(document.selection)//IE 8 and lower
    { 
        range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible)
        range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range
        range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
        range.select();//Select the range (make it the visible selection
    }
}

Open in new window

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now