I'm trying to use Javascript to make a text box that contains some read-only text at the beginning of the text box and then allows editing following the read-only text. How can I do this in Javascript/jquery?
Set the TextBox control's ReadOnly property to true . With the property set to true , users can still scroll and highlight text in a text box without allowing changes.
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.
Use the ReadOnly property to specify whether the contents of the TextBox control can be changed. Setting this property to true will prevent users from entering a value or changing the existing value. Note that the user of the TextBox control cannot change this property; only the developer can.
The Boolean readonly attribute, when present, makes the element not mutable, meaning the user can not edit the control. If the readonly attribute is specified on an input element, because the user can not edit the input, the element does not participate in constraint validation.
here is some thoughts
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/
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/
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 .
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With