简体   繁体   中英

Enable/disable textarea after taking input from keyboard

I have a textarea which is disabled by default. And then on press of 'Edit' I take some input from user. If it is valid, I want to enable the textarea. Here is the code which I have right now:

<textarea name="comment" cols="5" rows="2" disabled="true"><%= $tmp_com %></textarea>
<a href="javascript:validateUser()">Edit</a>

function validateUser(){
var name=prompt("Please enter the password");

    if (name=="1234")
    {
       document.getElementByName("comment").disabled="false";
    }
}

There is no getElementByName in JavaScript. Easiest solution, add an id, and use getElementById.

<textarea name="comment" id="comment" cols="5" rows="2" disabled="true">

and JavaScript

document.getElementById("comment").disabled="false";

Its better for you to use id instead of name. Any way I'm using name here to follow the question.

<a href="javascript:validateUser()">Edit</a>
<textarea name="comment" cols="5" rows="2" disabled="disabled">aaaaa</textarea>

<script type="text/javascript">
    function validateUser(){
        var name=prompt("Please enter the password");
        if (name=="1234")
        document.getElementsByName("comment")[0].disabled=false;
    }
</script>

Use jquery

$("[name='comment']").attr('disabled', true);
$("[name='comment']").attr('disabled', false);

or by Id

$("#comment").attr('disabled', true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM