Solved

certain text in string inside of TextArea is hioghlighted

Posted on 2014-01-05
5
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

749 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