开发者

jQuery select and deselect textbox text

I want functionality like on the focus event of text box i want to select the text of text box and on click of textbox i want to check if text in the textbox is selected then deselect it other wise select.

for this i have added code like:

开发者_StackOverflow中文版$('input').focus(function(){
  $(this).select();
});

what code i should add on click event so that it can fulfill the requirment

Thanks & Regards

Munish


If you want to give the user the ability to overwrite what's in there already, you could just set it blank instead:

$('input').focus(function(){
  $(this).val('').removeClass('default-value');
});

$('input').blur(function(){
  if ($(this).val()=='')
    $(this).val('default value').addClass('default-value');
});

The addClass/removeClass bits aren't required... they're just there so that you can make the default value show up as grey or italics or something if you wish. To me, that looks quite professional.


The jQuery dox here on the .select() method say the following:

The method for retrieving the current selected text differs from one browser to another. A number of jQuery plug-ins offer cross-platform solutions.

So on the face of it it looks like you'll have to go down the plugin route to determine whether text is selected or not inside your input control.

I would also echo Felix's caveat that what you are doing does interfere with user expectations of how an input-text element will behave when clicked and that you should think carefully about whether your requirements really indicate this is the way to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜