I am generating multiple forms using php code in a php page where each form has a textarea input and a button. Once a value will be entered the button will be enabled and its empty button will be disable. I found hard to trigger textarea and button using their id as php code is generating unknown number of forms in different type. Is there any other way to trigger form and it's button and textarea input to be able to disable/enable button?
<form method="POST" name="comment" class="setCommentBox">
<div class="form-group">
<div class="bs-example">
<textarea class="form-control" rows="2" name="txtcomment" maxlength="140" style="resize: none;" placeholder="Compose comment"></textarea>
<input name="txtHiddenMusicPostID" value="37" type="hidden">
</div>
</div>
<button type="submit" class="btn btn-success pull-right" name="post-Comment" value="post-Comment" disabled="disabled">
</button>
</form>
<form method="POST" name="comment" class="setCommentBox">
<div class="form-group">
<div class="bs-example">
<textarea class="form-control" rows="2" name="txtcomment" maxlength="140" style="resize: none;" placeholder="Compose comment"></textarea>
<input name="txtHiddenMusicPostID" value="37" type="hidden">
</div>
</div>
<button type="submit" class="btn btn-success pull-right" name="post-Comment" value="post-Comment" disabled="disabled">
</button>
</form>
<form method="POST" name="comment" class="setCommentBox">
<div class="form-group">
<div class="bs-example">
<textarea class="form-control" rows="2" name="txtcomment" maxlength="140" style="resize: none;" placeholder="Compose comment"></textarea>
<input name="txtHiddenMusicPostID" value="37" type="hidden">
</div>
</div>
<button type="submit" class="btn btn-success pull-right" name="post-Comment" value="post-Comment" disabled="disabled">
</button>
</form>
jqueryCode:
jQuery("document").ready(function ($) {
var $register = $("button[name='post-Comment']");
//$register.attr('disabled', true);
$("textarea[name='txtcomment']").keyup(function () {
var trigger = false;
$("textarea[name='txtcomment']").each(function() {
if ($(this).val() === '') {
trigger = true;
}
});
if (trigger) {
$register.attr('disabled', 'disabled');
} else {
$register.removeAttr('disabled');
}
});
});