jquery - How can I hook into a true "OnChanged" event for an HTML Textbox (Input type Text)? -
i have following jquery, intent of replacing alert custom validation code has been entered in textbox:
$(document).on("change", '[id$=txtbxssnoritin]', function () { alert('the textbox change event occurred'); });
however, event fires/the alert seen when "textbox" loses focus. entering value not invoke event; leaving control/element fires event.
should using event (other "change") or how can capture each change of content?
i don't want let user type in "warble p. mcgokle" , then, after tab out or click elsewhere, tell him/her/it value entered invalid. that's surefire way mugshot plastered onto center of company dartboard.
update
in jsfiddle, tymejv's script did work me; actually, changed to:
$(document).on("input", "textarea", function() { alert('you entered ' + this.value); });
...but doesn't work me (in project/webpart):
$(document).on("input", '[id$=txtbxssnoritin]', function () { alert('the textbox input event occurred'); });
(i see no alert while entering vals text element)
for context/full disclosure, here entire jquery (at end of *.ascs file):
<script type="text/javascript"> $(document).ready(function () { console.log('the ready function has been reached'); /* "sanity check" can verified jquery script running/ran */ }); /* when "employee" checkbox changes state, set txtbxssnoritin accordingly */ $(document).on("change", '[id$=ckbxemp]', function () { var ssnmaxlen = 4; var itinmaxlen = 12; var ssntextboxwidth = 40; var itintextboxwidth = 100; var ckd = this.checked; var $input = $('[id$=txtbxssnoritin]'); var $lbl = $('[id$=lblssnoritin]'); if (ckd) $input.data("oldvalue", $input.val()); // remember current value $input.prop("maxlength", ckd ? ssnmaxlen : itinmaxlen).css({ background: ckd ? 'yellow' : 'lightgreen', width: ckd ? ssntextboxwidth : itintextboxwidth }).val(function (i, v) { /* if checked, trim textbox contents ssnmaxlen characters */ return ckd && v.length > ssnmaxlen ? v.slice(0, ssnmaxlen) : $input.data("oldvalue"); }); $lbl.text(ckd ? "ssn - last 4 digits" : "itin"); /* sets focus end of textbox (multiplication 2 because characters counted two) */ var strlength = $input.val().length * 2; $input.focus(); $input[0].setselectionrange(strlength, strlength); }); $(document).on("input", '[id$=txtbxssnoritin]', function () { alert('the textbox input event occurred'); }); </script>
changing event "input" "keyup" works fine, though, combination of tymejv's script , various suggestions comments worked wonders. change answer "keyup" , i'll mark correctamundo.
you can use input
event fires whenever input modified (via paste, key stroke, etc)
$(document).on("input", '[id$=txtbxssnoritin]', function () {
Comments
Post a Comment