Auto Select All Content Within a Textarea Using Javascript

7/7/2014 2:54:02 PM

The following will highlight the text within a textarea upon clicking into the box.

<!-- jQuery is required. Remove the following line if you already are referencing jQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<textarea id="txtOutput" style="width: 500px; height: 300px; ">@this.Model.Output</textarea>

<script type="text/javascript">
	$("#txtOutput").focus(function()
	{
		var $this = $(this);
		$this.select();

		// Work around Chrome's little problem
		$this.mouseup(function ()
		{
			// Prevent further mouseup intervention
			$this.unbind("mouseup");
			return false;
		});
	});
</script>