Solved

certain text in string inside of TextArea is hioghlighted

Posted on 2014-01-05
5
227 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
ID: 39757876
you CAN'T change color of text inside a textarea
0
 
LVL 52

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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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 the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

914 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

14 Experts available now in Live!

Get 1:1 Help Now