Kevin
Kevin

Reputation: 1240

Highlighting Text in a Textbox Using Jquery

I have a web page that prompts for user input in a textbox. I would like to display a default line of text in this box but highlight it so that the user input will automatically overwrite it. I was told in another thread to use JQuery and this is what I have so far:

<script type="text/javascript" src="D:jquery-1.4.1-vsdoc.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#MessagesTextbox").select();
    });
</script>

Of course it is not doing the desired trick. I am very new to JQuery -- and web programming in general -- and I would appreciate any advice.

Regards.

Upvotes: 2

Views: 2032

Answers (3)

Mike
Mike

Reputation: 781

Try This - $('#MessagesTextbox').focus(function() { this.select(); });

Upvotes: 0

David Thomas
David Thomas

Reputation: 253476

Combine both select() and focus():

$('#MessagesTextbox').focus().select();​

JS Fiddle demo.

If you're able, though, it'd be even easier to use the placeholder attribute for the textarea:

<textarea id="MessagesTextbox" placeholder="Some default text"></textarea>​

JS Fiddle demo.

And the following jQuery:

References:

Upvotes: 2

The Jonas Persson
The Jonas Persson

Reputation: 1746

I'm not sure I understand exactly what you are after, but check this link out: watermark plugin. It's a jquery plugin for watermarks.

You'll have to download the jquery.watermark-3.1.3.js, and then all you do is

$(document).ready(function () {
    $("#MessagesTextbox").watermark('Required information');
})

Edit

I re-read it, and if you are just looking to put focus on the field, then yes, $("#MessagesTextbox").focus(); is what you are looking for, and added the link.

Upvotes: 0

Related Questions