By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

highlighting a searched word it text area

P: 1
Hi,

I want to highlight a searched word in the text area using javascript .
Jul 10 '08 #1
Share this Question
Share on Google+
3 Replies


hsriat
Expert 100+
P: 1,654
It might be something like:
Expand|Select|Wrap|Line Numbers
  1. /*JavaScript Code*/
  2. function highlight(word) {
  3.     var txt = document.getElementById('textDiv');
  4.     txt.innreHTML = txt.innerHTML.replace(' ' + word + ' ', ' <span class="highlight">' + word + '</span> ');
  5. }
  6. //where textDiv is the div which contains the text.
Expand|Select|Wrap|Line Numbers
  1. /*CSS code*/
  2. span.highlight {
  3.     background-color:yellow;
  4. }
See if this works.
Jul 10 '08 #2

Markus
Expert 5K+
P: 6,050
Do you mean highlight some text that's in a text area? Or highlight the text given through a text area?
Jul 10 '08 #3

rnd me
Expert 100+
P: 427
this will find and highlight the first one:
Expand|Select|Wrap|Line Numbers
  1. function showMatch(TXT, phrase){ 
  2.     var t= TXT.value;
  3.     var lp = t.split(phrase)[0].length;
  4.     TXT.selectionStart = lp
  5.     TXT.selectionEnd = lp + String(phrase).length;    
  6. }
TXT is an object: the textarea

phrase is the string (or regexp) to find.
Jul 11 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.