Reputation: 1240
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
Reputation: 781
Try This - $('#MessagesTextbox').focus(function() { this.select(); });
Upvotes: 0
Reputation: 253476
Combine both select()
and focus()
:
$('#MessagesTextbox').focus().select();
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>
And the following jQuery:
References:
Upvotes: 2
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