?
Solved

certain text in string inside of TextArea is hioghlighted

Posted on 2014-01-05
5
Medium Priority
?
232 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 53

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Find out what you should include to make the best professional email signature for your organization.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
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

801 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