Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML/JavaScript: How to make default text of a <textarea> undeletable?

Is there a way to make the default text of a <textarea> input become undeletable by the user? For example, if we had a <textarea> like this:

<textarea>I like/dislike this site because</textarea>

The user should be able to complete the "I like/dislike this site because" sentence but never be able to remove this part.

I understand there are easier approaches for this problem, but I've been asked to do it this way. Also, the target website uses prototype javascript framework so it must either involve prototype or no framework. jQuery cannot be used as it would cause conflict with prototype.

Many thanks in advance.

like image 497
Steve Avatar asked Mar 30 '11 18:03

Steve


People also ask

How to make textarea readOnly in JavaScript?

The readOnly property sets or returns whether the contents of a text area should be read-only. In a read-only text area, the content cannot be changed, but a user can tab to it, or highlight and copy the content from it. This property reflects the HTML readonly attribute.

What is the default value of textarea HTML?

Note: The default value of a text area is the text between the <textarea> and </textarea> tags.

How do you change text input to textarea?

Set the text value as the contents. Insert the textarea element into the DOM after the text box. Remove the text box. Give the code a try and post back with any problems that you have with it.

Can I add value to textarea?

To add a value into a textarea, you can add to the value property of the input via document. getElementById. Reference this element and change the value: document.


2 Answers

You could make it look like it's part of the textarea, even if it is not.

Simple jsfiddle example to demonstrate the idea

like image 87
Elian Ebbing Avatar answered Oct 07 '22 07:10

Elian Ebbing


I would fake it. It's dirty but you cannot delete the default text: http://jsfiddle.net/2EMkF/3/.

function $(id) {return document.getElementById(id);}
$('s').addEventListener('click', function() {
    $('t').setSelectionRange(33, 33)
    $('t').focus();
});
$('t').addEventListener('keyup', function() {
    if($('t').value.substring(0, 33) != ' '.times(33)) {
            $('t').value = ' '.times(Math.max(
                33, $('t').value.firstspace())
            )
            + $('t').value.fromnospaces();
            $('t').setSelectionRange(33,33);
    }
});

function t() {
    if(this.selectionStart < 33) this.setSelectionRange(33,33);
}

$('t').addEventListener('keyup', t);
$('t').addEventListener('click', t);

String.prototype.times = function(n) {
    var r = "";
    for(var i = 0; i < n; i++) r += this;
    return r;
}

String.prototype.firstspace = function() {
    for(var i = 0; i < this.length; i++)
        if(this[i] != ' ')
            return i;
    return -1;
}

String.prototype.fromnospaces = function() {
    return this.substring(this.firstspace());
}
like image 2
pimvdb Avatar answered Oct 07 '22 07:10

pimvdb