javascript - Highlight selected text with jquery -



javascript - Highlight selected text with jquery -

when user selects text in html page want add together custom style (like color:red;) it. deed highlighting tool similar can see in applications reading pdf files.

to declare highlight() function gets text selected, plus position.

function highlight(text, x, y) { alert(text+'***'+x+'***'+y) window.getselection().removeallranges(); }

jsfiddle link edited

jsfiddle

after research suggest go way.

html

<h3 class="foo">hello world! hello world! hello world</h3> <div class="foo">hello world! hello world hello world!</div> <span class="foo">hello world! hello world</span>

css

.foo::selection { color:#ff0099; } .bar::selection { color: red; }

js

$(document).ready(function(){ $(".foo").removeclass("foo").addclass("bar"); });

fiddle

first add together class in element want utilize effect. after utilize selection selector , js add , remove classes. hope helps :)

javascript jquery selection

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -