How to make only some text in a text-box read-only while allowing the rest to be edited

Here's an attempt:

var readOnlyLength = $('#field').val().length;

$('#output').text(readOnlyLength);

$('#field').on('keypress, keydown', function(event) {
  var $field = $(this);
  $('#output').text(event.which + '-' + this.selectionStart);
  if ((event.which != 37 && (event.which != 39)) &&
    ((this.selectionStart < readOnlyLength) ||
      ((this.selectionStart == readOnlyLength) && (event.which == 8)))) {
    return false;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input id="field" type="text" value="CAN'T TOUCH THIS!" size="50" />
<div id="output">
</div>

This disables keys other than the left and right arrows for the read-only part. It also disables the backspace key when just at the end of the read-only part.

From what I've read, this won't work on IE <= 8.


Here it is in plain JavaScript (no JQuery):

function makeInitialTextReadOnly(input) {
  var readOnlyLength = input.value.length;
  field.addEventListener('keydown', function(event) {
    var which = event.which;
    if (((which == 8) && (input.selectionStart <= readOnlyLength)) ||
      ((which == 46) && (input.selectionStart < readOnlyLength))) {
      event.preventDefault();
    }
  });
  field.addEventListener('keypress', function(event) {
    var which = event.which;
    if ((event.which != 0) && (input.selectionStart < readOnlyLength)) {
      event.preventDefault();
    }
  });
}

makeInitialTextReadOnly(document.getElementById('field'));
<input id="field" type="text" value="CAN'T TOUCH THIS!" size="50" />


here is some thoughts

roughly a solution with jquery

html

<input type="text" id='myinput' value ="my text">

jquery

var orginal_text = $('#myinput').val();
var regular_expression = '/^' + orginal_text +'/' ;
$('#myinput').keyup(function(){
    var current_text = $('#myinput').val();
    if(current_text.match('^' + orginal_text +'') == null){
        $('#myinput').val(orginal_text + ' ' +current_text )
    }

})

http://jsfiddle.net/e5EDY/

with css

html

 <input type="text" id='my_sticky_text' value ="my text" readonly='readonly'>
<input type="text" id='myinput' value ="my text">

css

#my_sticky_text{
    position:absolute;
    left : 0px;
}

#myinput{
    position:absolute;
    left : 50px;
    border-left:none;
}

http://jsfiddle.net/kaf4j/

combination to retrieve the value

html

 <input type="text" id='my_sticky_text' value ="my text" readonly='readonly'>
<input type="text" id='myinput' value ="my text">
    <br>
    <hr>
    <button id='getval'>get value</button>

css

#my_sticky_text{
    position:absolute;
    left : 0px;
}

#myinput{
    position:absolute;
    left : 50px;
    border-left:none;
}

jquery

$('#getval').click(function(){
   var sticky_text = $('#my_sticky_text').val();
    var user_text = $('#myinput').val();
alert (sticky_text + ' ' + user_text)

})

http://jsfiddle.net/YsNMQ/

what do we get from all this ?!.. simply you cant acomplish what you want in a nice way .. and i cant imagine a situation where i want to do so .

alternatives

1 - in the text-field label put the constant text .

2 - when the user submits the form capture the value of the text-field and add your text to it .

3- add a help note to the user that this input should be as follows (eg : mytext-yourtext)


Below is a modified version of John S's answer to prevent cut/paste operations (e.g. via right-click):

function makeInitialTextReadOnly(input) {
    var readOnlyLength = input.value.length;
    input.addEventListener('keydown', function(event) {
        var which = event.which;
        if (((which == 8) && (input.selectionStart <= readOnlyLength))
                || ((which == 46) && (input.selectionStart < readOnlyLength))) {
            event.preventDefault();
        }
    });
    input.addEventListener('keypress', function(event) {
        var which = event.which;
        if ((event.which != 0) && (input.selectionStart < readOnlyLength)) {
            event.preventDefault();
        }
    });
    input.addEventListener('cut', function(event) {
        if (input.selectionStart < readOnlyLength) {
            event.preventDefault();
        }
    });
    input.addEventListener('paste', function(event) {
        if (input.selectionStart < readOnlyLength) {
            event.preventDefault();
        }
    });
}

makeInitialTextReadOnly(document.getElementById('field'));

Fiddle: http://jsfiddle.net/p0jyktvu/3/