Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selectable but not editable html text field

Tags:

html

input

I am working on a mobile website and I have a text input field.

I want it to be selected and copiable but not editable. When I add readonly or onfocus="this.blur()" it becomes unselectable. How can I achieve this?

like image 387
tasomaniac Avatar asked Jan 23 '13 12:01

tasomaniac


People also ask

How do you make text fields not editable in HTML?

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. Setting the value to null does not remove the effects of the attribute. Instead use removeAttribute('readonly') .

How do I make a textbox not clickable in HTML?

Try disabled="disabled" . This will disable textbox / textarea, so it won't be selected. Also, the value won't be submitted on form submission. In HTML5, only disabled attribute will also work.

How do you make an input type editable in HTML?

All you have to do is set the contenteditable attribute on nearly any HTML element to make it editable.


1 Answers

Check this out.

<textarea rows="10" cols="50" onclick="this.focus();this.select()" readonly="readonly">
    example text
</textarea>

Edit:

You can reassign text input value everytime it changes by adding input listener.

var inp = $("input")[0]; // select the input with proper selector
var default_value = inp.value;

inp.addEventListener("input", function () { 
    this.value = default_value;
}, false);

Working jsfiddle here.

like image 59
Krzysztof Jabłoński Avatar answered Oct 27 '22 22:10

Krzysztof Jabłoński