Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Vanilla JS class to highlight search results in a textarea while maintaining the area's functionality

License

NotificationsYou must be signed in to change notification settings

wstaeblein/texthighlighter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vanilla JS class to highlight search results in a textarea while maintaining the area's functionality.

Since one can't style the text in a textarea, as it is not HTML, this class comes to the rescue allowing you to highlight any text in a textarea. Such as in search results.

Screenshot

CHECK OUT THE DEMO{:target="_blank"}

Features

  • No dependencies.
  • No configuration. Just instantiate the class and call a method.
  • Can be used in as many textareas as needed, just instantiate for each one.
  • Responsive. The markings will adjust to screen size, textarea size and scroll.
  • Keeps the original background from the textarea.
  • Can be properly destroyed. Will remove all events and structure.
  • Can choose between case sensitive and insensitive search.
  • Can search freely or words only.
  • Provides a count of how many results were found.
  • Can navigate the search results, bringing them to view as needed.

Its use is very straightforward, instantiate the class for the textarea you need and call the search method to highlight the text you pass in the first argument. The second argument is optional and takes a boolean that if true will make the search case sensitive. The default is case insensitive. The last argument also takes a boolean that, if true, will perform a word search. Otherwise it's a free search, where any part of words can be matched.

Usage & Code Examples

Add the following files to your project:

  • texthighlighter.js
  • texthighlighter.css
lettarea=document.getElementById('txt');lethilite=newtextHighlight(tarea);letsearchResult='Some Expression';letsens=true;// Case sensitive. Optional, default: falseletword=true;// Perform a words only search. Optional, default: falsehilite.search(searchResult,sens,word);

To access how many occurrences were found, use:

letcount=hilite.count();

You can navigate back and forth among the highlighted results using the prev and next methods. This navigation is cyclic, this means when the end is reached the next call takes you back to the begining and vice versa. Example below:

letbtnPrev=document.getElementById('prev');letbtnNext=document.getElementById('next');btnPrev.addEventListener('click',hilite.prev);btnNext.addEventListener('click',hilite.next);

Should you need to clear out the highlights, call the clear method.

hilite.clear();

When it falls out of scope, just call the destroy method and all will be as it was before instantiation.

hilite.destroy();

About

This class was inspired by lonekorean'shighlight-within-textarea JQuery plugin. Basically, I needed a similar functionality for a project but didn't want to include JQuery just for that and didn't find any other such code that was good enough.

The main aim here was to provide highlighting for search results in a textarea.

This class was tested in Chrome 117 and Firefox 118.

Licence

This project is licensed under theMIT Licence

About

Vanilla JS class to highlight search results in a textarea while maintaining the area's functionality

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp