[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

certain text in string inside of TextArea is hioghlighted

Posted on 2014-01-05
5
Medium Priority
?
235 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
3 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 39757876
you CAN'T change color of text inside a textarea
0
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 39758018
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 2000 total points
ID: 39761202
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

834 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