2

I am trying to disable the textbox using keyup functionality. I have a TextArea and a Text Box. Now i use a keyup operation on backspace key, like if the length of content inside textarea is 3 it should disable the textbox. I also have an alert message which pops when the length of content in text area is 3. Code worked for the pop up but it doesnot worked for the textbox. What am i missing? Please help. Here is my code:

$('#comment').keyup(function() {
    if (event.which == 8) {
           var txt = $('#comment').val().length;
            if(txt == 3)
            {
                alert("backspace");
                $("#text1").attr("diasbled", "diasbled");
            }
        }

});

And here is the JSfiddle for the purpose.

1
  • such a silly mistake i have done. Thank you guys. :) Commented Apr 24, 2014 at 7:21

4 Answers 4

5

You have some typo here it should be disabled not diasbled Try this

$('#comment').keyup(function () {
    var len = $(this).val().length;
    if (len >= 3) {
        $("#text1").prop("disabled", true);
    }
    else{
     $("#text1").prop("disabled", false);
    }
});

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

if i want to do the same on backspace event then how to do it. can you please explain me that too.
No need to handle backspace here, just put the else condition, see my updated answer
if u are using version earlier than Jquery 1.6 then You will have to use .attr (like $("#text1").attr("disabled", true);) instead of .prop @nrsharma am i right ??
3

You need to do:

1) this.value.length to get the total characters length of your textarea

2) From jQuery version 1.6 , use .prop() instead of .attr() to set the properties of an element

3) Correct the typo: it should be disabled not diasbled

$('#comment').keyup(function () {
    if (this.value.length >= 3) {
        $("#text1").prop("disabled", true);
    } else {
        $("#text1").prop("disabled", false);
    }
});

Updated Fiddle

Comments

1

Your code is fine.. but you mispelled the "disabled" in your code. Here's the sample..

<html>
<head>
<title>js test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
</head>
<body>
<input type="text" value="" id="comment" />
<input type="text" value="" id="text1" />
<script type="text/javascript">
$(document).ready(function()
{
    $('#comment').keyup(function(e) {
        if (e.which == 8) {
               var txt = $('#comment').val().length;
                if(txt == 3)
                {
                    alert("backspace");
                    $("#text1").attr("disabled", "disabled");
                }
            }

    });
});
</script>
</body>

Comments

1

Use prop instead of attr also pass event to function

 $('#comment').keyup(function (event) { //and event here
    if (event.which == 8) {
        if ($(this).val().length >= 3) {
            $("#text1").prop("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.