1

I have the following function which limits the amount of characters you can enter into a text area.

What's meant to happen is, if the text length is higher than the text limit, set an attribute of disabled="disabled" on the submit button, but it isn't getting set, my function code is as follows:

function limitChars(textid, limit, infodiv) {
        var text = $('#'+textid).val(); 
        var textlength = text.length;
        if(textlength > limit) {
            $('#' + infodiv).html('You cannot write more then '+limit+' characters!');
            $('input#submit').attr('disabled', 'disabled');

        } else {
            $('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');

        }
    }

It's specifically the $('input#submit').attr('disabled', 'disabled'); bit that isn't working.

Any ideas?

2
  • I'm absolutely certain this is a duplicate, I remember answering a very similar question a few weeks ago (and another before that). I can't find it though... Commented Apr 28, 2010 at 15:49
  • Not from me, I did a quick search before asking and found nothing similar. Commented Apr 28, 2010 at 19:27

5 Answers 5

1

Use $('input#submit').attr('disabled', true);. If you need to enable it again, use

.removeAttr('disabled');
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Thanks for your response. I gave that a go using true, rather than disabled, and no luck.
0

I posted a demo and it appears to work with this code:

$(document).ready(function(){
 $('#test').keyup(function(){
  limitChars( this.id, 20, 'info')
 })
})

function limitChars(textid, limit, infodiv) {
 var text = $('#'+textid).val(); 
 var textlength = text.length;
 if(textlength > limit) {
  $('#' + infodiv).html('You cannot write more then '+limit+' characters!');
  $('input#submit').attr('disabled', 'disabled');
 } else {
  $('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
  $('input#submit').removeAttr('disabled');
 }
}

4 Comments

strangely enough I tried your demo and it didn't work for me, the attribute didn't appear
I'm confused, the attribute is set in the code and the submit button should become disabled when more than 20 characters are typed in the textarea, and re-enabled if there are 20 or less characters in the textarea. What do you mean by "the attribute didn't appear"?
Hmmm. if you mean you are using firebug and you don't see the attribute appear, then you shouldn't worry about it. The attribute is being updated in the DOM and may not be added directly to the HTML tag in the firebug window.... you should see the submit button become disabled still!
Hi, that's exactly what was happening, I was expecting firebug to detect the 'disabled="disabled"', which it did not, but turns out it all works fine. Thank you very much :)
0

Do you see your info message 'you cannot write more.....'?

Try and change your selector to ':input#submit'. Make sure the submit button's ID is "submit" and remember, IE is case sensitive.

1 Comment

Hi, Thanks for your response. Yes the 'You cannot write more' message appears. Also I tried changing the selector and still no luck. The input definately has the id of submit too
0

if referencing by element id, just use $("#submit") is enough, I think

Comments

0

Your function works. How are you binding the textarea? You should be usign keyup:

$('#textarea').keyup(function() {
limitChars('textarea', 4, 'info');
});
limitChars('textarea', 4, 'info'); // run this initially as well

1 Comment

Hi, Yep I'm using keyup to bind the textarea.

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.