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

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.


Visual Studio 2008 Shell and TFS integration

Visual Studio 2008 Shell and TFS integration is the problem for all newbies of BIDS and TFS. Here is the solution.


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.


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.


Best CSS Gradient background generator tools online

Here are some best CSS gradient background code generator online tools using which you can create a cross browser css code for gradient backgrounds.


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!
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