Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here


Highlight text on focus for all input textboxes using jQuery

Share with WhatsApp

Hello friend, in this post I am going to share simple code by which you can select or highlight a text of all input textboxes of your HTML page.

If you are already using jQuery and has advanced knowledge of it then you might found this code very easy but for freshers or newbies in jQuery will definetely get benefited by this.

Let's start.

You might know that we have an event called "onfocus" which gets triggered when input gets foucs on it. I am using this event to select or highlight the text by calling simple inbuilt method of jQuery called "select()" like below code.

$(document).ready(function () {
    $("input:text").focus(function () { $(this).select(); }); 

Though above code works pretty well with IE and Firefox but it may not work on Google Chrome. In Chrome, If you observe the behaviour with above code then you will find that the text gets selected on focus but immediatly the cursor comes at end which removes the selection. So to make this code work in Chrome too you need to add one more line of code to handle mouseup event like below which is creating problem.

$(document).ready(function() {
   $("input:text").mouseup(function(e){ e.preventDefault();});
   $("input:text").focus(function () { $(this).select(); }); 

Check out the live fiddle below.

You might thinking what is the use of this then the answer is sometimes user of the application may need to provide inputs several times in same textbox in that case he can just type a new text which will overrite the selected text without need of explicitly clearing the existing text with backspace key or delete key.

Hope this simple code has added something positive in your knowledge bank. If you too having such useful code do share with us in comment section or by visitng our contact us page. Thanks.

If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

What is Hoisting in JavaScript?

Hoisting in JavaScript, one of the most misunderstood or little understood concept in the JavaScript and remain famous as interview question for JavaScript developers and now also for angular js developer. Lets check out what it means exactly.

Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.

Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.

How to post JSON string as a string in jQuery AJAX

Know how to post JSON String as string to the action method of controller in MVC.

How to call click or any event only once in jQuery

Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.

Your opinion is valuable for us! Comments, suggetions are welcome.

Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
Vu 32'' TV Vu 24'' TV
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!