Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add non-editable text after cursor in input box

I'm trying to add non-editable text after the cursor in a text field/input box as to achieve the following:

1) The text before the cursor is editable 2) The text after the cursor is not editable and moves right/left as the text to the left is added/removed

INPUT FIELD: [editabletexthere{cursorhere}non-editabletexthere ]

Here is the code to my input box thus far:

<input id="edit_input" type="text" autocapitalize="off" autocorrect="off" spellcheck="false" autofocus onfocus="this.value = this.value;" value="editabletext" style="width:100%;"></input>

I feel like the values in the input should be housed within a span and for the second span simply to be stylized with contenteditable= false;. I would also like to stylize the non-editable text to a different color. Any help would be much appreciated. Thank you.

like image 774
Collarbone Avatar asked Sep 08 '14 20:09

Collarbone


People also ask

How do I make my input box non-editable?

The readonly attribute is a boolean attribute. When present, it specifies that an input field is read-only. A read-only input field cannot be modified (however, a user can tab to it, highlight it, and copy the text from it).

How do you make text fields non-editable in CSS?

The read-only attribute in HTML is used to create a text input non-editable. But in case of CSS, the pointer-events property is used to stop the pointer events.


2 Answers

This might not be the best solution, it's kind of a hack but it works fine. The idea is to create a div that looks like an input box with inside 2 span, and you make one of them editable.

div {
    border: 1px solid #999;
    padding: 5px;
    width: 100%;
}

span {
    width: auto;
    outline: 0;
}

span.trailing {
    color: grey;
}
<div>
    <span contenteditable="true" id="edit_input_fake">Input</span>
    <span class="trailing">Trailing text</span>
  </div>

You can create an <input type="hidden"> where you copy the content of the first span (maybe the second one as well?) on event keydown/change if needed.

JSFiddle available here: http://jsfiddle.net/c6f7ommh/1

like image 167
alexmngn Avatar answered Sep 28 '22 11:09

alexmngn


JSFiddle - DEMO

You could do it like this:

HTML:

<input id="edit_input" type="text" autocapitalize="off" autocorrect="off" spellcheck="false" autofocus onfocus="this.value = this.value;" value="editabletext" style="width:100%;"></input>
<p>SOME TEXT</p>

CSS:

input {
    width: 100%;
    float:left;
    font-size: 16px;
    padding: 0px 120px 0px 0px;
    box-sizing:border-box;
}
p {
    margin:0px 0px 0px -100px;
    float:left;
    padding-top: 5px;
}

[EDITED]

If you want to cursor: default; and user-select: none; - DEMO

like image 35
Anonymous Avatar answered Sep 28 '22 12:09

Anonymous