Search code examples
javascriptjqueryhtmldisabled-input

Prevent typing in Text Field Input, Even Though Field is NOT Disabled/Read-Only


Can I prevent HTML Text Field input even when my field is NOT Disabled or Read-only? I have this requirement.

Maybe I can block all inputs via JS or jQuery?


Solution

  • See this fiddle

    You can use jQuery for this.. You can do it as below

    $('input').keypress(function(e) {
        e.preventDefault();
    });
    

    OR

    you can just return false instead of using preventDefault(). See the script below

    $('input').keypress(function(e) {
        return false
    });
    

    See the fiddle

    OR

    A much simplified version without Javascript would be as below. Just change your HTML as below

    <input type="text" onkeypress="return false;"/>
    

    See the fiddle