1

I am trying to get a function to work so that I get an alert (for now!) when a user has entered a four character code into a text box.

I'm using...

$(window).load(function () {
$("#textbox").on('input', function () {
    if ($("#textbox").val().length > 3) {
        alert("Four characters detected!");
    }
});

});

I must be missing something, as the alert doesn't fire. JSFiddle at: http://jsfiddle.net/1s8phz4w/

TIA JV

JohnV
  • 37
  • 7

2 Answers2

0

Probably your js is loading before the element. Prevent this using $(document).ready(function(){ your code here });

This will trigger your function only after your page is fully loaded.

Matheus Godoy
  • 72
  • 1
  • 5
0

I think the 'input' event is not the one you're seeking.

Try 'change' or 'keyup' :

$(document).on('ready', function () {
    $('#textbox').on('change keyup', function () {
        if (this.value.length > 3) {
            alert("Four characters detected!");
        }
    });
});
Monkey Monk
  • 984
  • 9
  • 19
  • I went for the 'input' event as it covers a number of different types on input including right-click paste etc. as I found here: [link]http://stackoverflow.com/questions/17317465/jquery-textbox-change-event-doesnt-fire-until-textbox-loses-focus – JohnV Sep 20 '14 at 07:45