javascript - jQuery set button element active and disabled dynamically -
i'm working on client side script using jquery.
i achieve set button element disabled or enabled, when enter or delete text in 1 input field.
however, need check if 2 input fields have value, otherwise buttons should disabled. though, buttons enabled if put text in 1 input field...
html
<button type="button" id="btnsave">save</button> <button type="button" id="btnsubmit">save</button> <input type="text" id="businesslineidlist" /> <input type="text" id="label" />
js
$("#businesslineidlist").on("keyup", function () { checkbusinessline(); checklabel(); }); $("#label").on("keyup", function () { checklabel(); checkbusinessline(); }); function checklabel() { if ($("#label").val()) { setbtnactive(); } else { setbtndisabled() }; } function checkbusinessline() { if ($("#businesslineid").val()) { setbtnactive(); } else { setbtndisabled(); } } function setbtnactive() { $("#btnsave").removeattr("disabled"); $("#btnsubmit").removeattr("disabled"); }; function setbtndisabled() { $("#btnsave").attr('disabled', true); $("#btnsubmit").attr('disabled', true); };
do have idea how solve issue? thanks
every time 1 has changed, check both of them , act according result of that:
$("#businesslineidlist").on("keyup", callback); $("#label").on("keyup", callback); function callback() { if (islabelhasvalue() && isbusinesslinehasvalue()) { setbtnactive(); } else { setbtndisabled(); } } function islabelhasvalue() { return $("#label").val() && $("#label").val().length > 0; } function isbusinesslinehasvalue() { return $("#businesslineid").val() && $("#businesslineid").val().length > 0; } function setbtnactive() { $("#btnsave").removeattr("disabled"); $("#btnsubmit").removeattr("disabled"); }; function setbtndisabled() { $("#btnsave").attr('disabled', true); $("#btnsubmit").attr('disabled', true); };
Comments
Post a Comment