[RESOLVED] How to select all the text in a Textbox in JQuery
Good Day All
i have a Jquery code defined like this
Code:
// Let's add it to textarea this time
$(".cnt").focus(function()
{
// Check for the change
if(this.value == this.defaultValue){
this.select();
}
});
and i am creating a textbox on fly and after creating it i bind data to it and after that i want to attach a focus event if there a value "0" on it
Code:
If cnt.Text = "0" Then
cnt.CssClass = "cnt"
End If
but still when i select a textbox that has "0" it does not select the whole content of the textbox. i went through a breakpoint and it goes through this line
Code:
cnt.CssClass = "cnt"
Thanks
Re: How to select all the text in a Textbox in JQuery
If the TextBox you're creating is actually created after the page has been loaded, then this won't work just because the focus event you're creating in JavaScript is created when the page loads. Any elements created after that JavaScript has been executed won't have that focus event attached to it. As long as you're using jQuery >= 1.4.1, you can use jQuery's live() event attachment function, however, which will attach an event to your selector to any elements that exist as it's called, as well as any elements that match that selector in the future.
Here's an example:
Code:
$('.cnt').live('focus', function(){
// Check for change
if(this.value == this.defaultValue){
this.select();
}
});
Hope that helps. If it doesn't, please provide a bit more information and possibly a bit more code to better describe what you're trying to do.
Re: How to select all the text in a Textbox in JQuery
Thanks guys for the reply, to resolve this i applied this to all the textboxes like this
Code:
$(document).ready(function()
{
$("input:text").focus(function()
{
$(this).select();
}
);
});
Thanks