javascript - How to check if text is highlighted (cross-browser) -
is there generic way in jquery or javascript (most cross-browser compatible) check if text has been highlighted?
i working html <input type='text'>
element. search box allows specific formats while searching project.
a user can input following:
project name, location, description, , other alpha numeric input project number (all numeric)on keydown filtering various search formats. stands, if there 10 characters entered, , numeric, input
textbox allow more characters entered if non-numeric. reason is, our project numbers 10 digits long, , in case user searching project number, want capture first 10 characters , ignore rest.
my goal allow users find project project number, highlight text entered, , search project it's project number (replacing existing search text). however, because filtering non-numeric input, numbers aren't captured.
here how filtering non-numerics:
if ($input.val().length == 10 && !isnan($input.val().replace(' ', '#'))) { if ((e.which >= 48 && e.which <= 57) || (e.which >= 96 && e.which <= 105)) { homecoming false; } }
how can modify ensure if text highlighted, input
allow numeric input replace existing query?
you can utilize selection
object :
var selection = window.getselection();
see documentation here : https://developer.mozilla.org/fr/docs/dom/selection
with object, can check selected dom node (anchornode
). illustration :
if ($(window.getselection().anchornode).attr('id') === '#something') { ... }
javascript jquery regex
No comments:
Post a Comment