Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the problem on this jquery form that checks for number of chars in a textarea?

this is a code that enables the submit button if there are more than 100 chars in the textarea. However I can't get it work. Maybe the jquery version is wrong? I don't know.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<form>
  <textarea id="textareaId"></textarea>
  <input type="submit" id="submitId" disabled="disabled" />
</form>
<script type="text/javascript">
  setInterval(function () {
    if(("#textareaId").val().length > 100) {
      $("#submitId").removeAttr("disabled");
    } else {
      $("#submitId").attr("disabled", "disabled");
    }
  }, 500); //Runs every 0.5s
</script>
like image 863
EnexoOnoma Avatar asked Dec 09 '22 09:12

EnexoOnoma


2 Answers

You're missing a $ in front of ("#textareaId")

like image 86
Marek Karbarz Avatar answered Jan 04 '23 22:01

Marek Karbarz


you are missing the $ by ("#textareaId")
and try using an onchange instead of a setInterval this might be more efficient:

$("#textareaId").change(function () {
   if((this.value).length > 100) {
     $("#submitId").removeAttr("disabled");
   } else {
     $("#submitId").attr("disabled", "disabled");
   }
}).keyup(function(){$(this).change()});

fiddle: http://jsfiddle.net/maniator/7Kch9/

like image 36
Naftali Avatar answered Jan 04 '23 22:01

Naftali