Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

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

Typewriter - Automatic TypeScript Template generation from C# classes in Visual Studio

Typewriter, an extension for visual studio that generates TypeScript files from C# code files using TypeScript templates. Convert your C# model classes to TypeScript automatically.


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.


Assembla - Free and private repository to manage your source code online with SVN subversion hosting

With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.


Top 10 Visual Studio things which can boost developers coding speed

Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.


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!

x