TiagoM
TiagoM

Reputation: 3526

Prevent From Writing on TextArea using Bind Event "input propertychange"

I am handling the content inside a textarea using binding a function to the event "input propertychange"

Like this:

$('#textarea').bind('input propertychange', function () {    
    var textarea = document.getElementById('textarea');    
    window.lastLineWriting = textarea.value.substr(0, textarea.value.length).split("\n").length;                    
    var writingOnLine = textarea.value.substr(0, textarea.selectionStart).split("\n").length;                  
    if (writingOnLine < window.lastLineWriting) {
        //dont write on textarea
    }
});

I don't know how to prevent the char typed by the user's keyboard to appear on the textarea... Inside that if I want to prevent the text to be inserted on textarea.. How can I do this?

Upvotes: 0

Views: 627

Answers (2)

rdanusha
rdanusha

Reputation: 923

 var editable = false // Your Condition
    if(editable != "true"){
        $("#textarea" ).attr("disabled",true);
     }    

Upvotes: 0

Amin Jafari
Amin Jafari

Reputation: 7207

you could easily stop the user from typing with this code, using jQuery:

$('textarea').bind('keypress', function (e) {
    e.preventDefault();
    return false;
});

NOTE:

this code will prevent the user from typing in all the textareas, to bind it specifically to one or some selected elements, you must change the selector to the desired elements.

Upvotes: 1

Related Questions