Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select the text in the input field, but the editing is disabled

I have a simple txt input field:

<input type="text" name="" value="http://google.com" />

If i click inside the input, i want to select the text. This part is fine with this:

$('input.highlight').click(function(){

$(this).focus().select();

return false;

});

But, i want to disable the editing also. If i put a return false; to the keydown event, the ctrl+c is not working. If i put disabled="disabled" to the input, the select is not working.

Any idea? Thanks.

like image 752
passatgt Avatar asked Mar 21 '11 20:03

passatgt


People also ask

How do I disable input field editing?

You can either use the readonly or the disabled attribute. Note that when disabled, the input's value will not be submitted when submitting the form. Also It's important to remind that even using readonly attribute, you should never trust user input which includes form submissions.

How do I make a text input non editable for a part of it?

The readonly attribute makes a form control non-editable (or “read only”). A read-only field can't be modified, but, unlike disabled , you can tab into it, highlight it, and copy its contents.


1 Answers

You want to add the readonly attribute instead of disabled:

<input type="text" name="" readonly="readonly" value="http://google.com" />
like image 157
scunliffe Avatar answered Nov 15 '22 07:11

scunliffe