6

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";
    }
}

3 Answers 3

13

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";
Sign up to request clarification or add additional context in comments.

1 Comment

Almost. Remove the quotes around "false" to make the argument a boolean and not a string, and you'll have it.
8

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>

1 Comment

...And a string for the value "false" - Better to use true/false without strings as these are numbers really.
4

Use jquery

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

or by Id

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.