I’m performing some form of validation on a freely typed input text field in HTML. Is there a way to highlight certain words or phrases (based on certain criteria) in a input text field using JQuery or JavaScript?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Within a text input box, your only opportunity for highlighting just a part of the text is using the selection. You can do this in all modern browsers with the text box’s
selectionStartandselectionEndproperties, orsetSelectionRange()method (no idea why both exist).Live demo: http://jsfiddle.net/YNr7K/
Code:
In older versions of IE (< 9), you’ll need to use one of their tiresome
TextRanges. See this answer for a function that shows how to do this.