4

I have HTML code like

<input type="text" class="input-box">

I want to execute a jQuery function when the inputbox is filled with 4 character.

For example, I type "ABCD" then the function will be executed and not for "AB" or "ABC" something like that. How can I do this?

1

1 Answer 1

5

Bind an event handler and check text length, based on the length call the function

$('.input-box').on('input', function() {
  if (this.value.trim().length >= 4) {
    // call your function here
    doIt();
  }
});

function doIt() {
  alert('hi');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" class="input-box">

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, @pranav. Got what I want.
@ShafeequeS : glad to help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.