I've been trying to get this code to work and everything seems correct but I can't get attr - "disabled" to be removed. The button disables initially but then when the fields are populated the "else" part does not fire. I thought this should execute after every "change", but it appears to not loop through the inputs correctly.
static_pages.js.coffee---
jQuery ->
$("#gradesModal :input").on('keypress', ->
empty = false
$("#gradesModal :input").each ->
empty = true if $(this).val() is ""
if empty
$("#add-grade").attr "disabled", "disabled"
else
$("#add-grade").removeAttr "disabled"
)
html---
<div class="form-horizontal">
<div class="control-group integer optional grade_student_id"><label class="integer optional control-label" for="grade_student_id">Student</label><div class="controls"><input class="numeric integer optional" id="grade_student_id" name="grade[student_id]" step="1" type="number" /></div></div><br />
<div class="control-group integer optional grade_lesson_id"><label class="integer optional control-label" for="grade_lesson_id">Lesson</label><div class="controls"><input class="numeric integer optional" id="grade_lesson_id" name="grade[lesson_id]" step="1" type="number" /></div></div><br />
<div class="control-group select optional grade_score"><label class="select optional control-label" for="grade_score">Score</label><div class="controls"><select autofocus="autofocus" class="select optional" id="grade_score" name="grade[score]"><option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option></select></div></div><br />
<div class="control-group text optional grade_comment"><label class="text optional control-label" for="grade_comment">Comment</label><div class="controls"><textarea class="text optional" cols="40" id="grade_comment" name="grade[comment]" rows="2"></textarea></div></div>
<input class="btn" id="add-grade" name="commit" type="submit" value="Create Grade" />
</div>
Compiled js
jQuery(function() {
return $("#gradesModal :input").on('keypress', function() {
var empty;
empty = false;
return $("#gradesModal :input").each(function() {
if ($(this).val() === "") {
empty = true;
}
if (empty) {
return $("#add-grade").attr("disabled", "disabled");
} else {
return $("#add-grade").removeAttr("disabled");
}
});
});
});