Click and Highlight Text in a TextBox using jQuery




Here’s a simple script that highlights the Text in a TextBox using jQuery, when the user clicks inside it. This technique of highlighting text is useful in forms when there are frequent copy paste operations.

<html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
title>Click and Highlight Text</title>
<
script src="http://code.jquery.com/jquery-latest.js"
type="text/javascript"></script>
<
script type="text/javascript">
$(function() {
$("#Text1").click(function() {
$(this).focus();
$(this).select();
});
});
</script>
</
head>
<
body>
<
input id="Text1" type="text" value="There is some text here"/>
</
body>
</
html>
image 

Here’s a Live Demo



Will you give this article a +1 ? Thanks in advance




About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

comments

1 Response to "Click and Highlight Text in a TextBox using jQuery"
  1. Ante said...
    August 24, 2009 at 6:05 AM

    One of jQuery's cool features is nested function-calls. The code could be written as follows instead:

    $(this).focus().select();

    This avoids calling the jQuery-function (or $) twice as well as saving some bytes.

 

Copyright © 2009-2014 All Rights Reserved for DevCurry.com by Suprotim Agarwal | Terms and Conditions