2

Hello I have an array of input text they all have the same class, so I want to put an event OnChange with jquery to everyone of them.

My HTML code is:

 <input type="text" class="form-control placementTest" data-mask=''>

and my Javascript is:

$('.placementTest').each(function() {
            $(this).on('change',function (ev) {
                alert('done it');
            });
        });

but it's not working. So, what is wrong?

2
  • does the code run after the DOM is loaded? Commented Dec 14, 2015 at 2:20
  • Should work just fine -> jsfiddle.net/cet0k4tu probably missing DOM ready or something Commented Dec 14, 2015 at 2:25

3 Answers 3

2

No Need for $('.placementTest').each . Also, with an input, you want the keyup event Just need

   $('.placementTest').keyup(function() {
      //do stuff

   })

or fire when the user leaves the input:

 $('.placementTest').blur(function() {
 //do stuff

 })

Fiddle http://jsfiddle.net/qpu0Lsth/2/

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

1 Comment

On a side note, why do you need to listen for change? What are you trying to accomplish? keyup will fire every entry. Do you want it to fire when the user leaves the input and goes to the next one? If so, use the blur()
1

your event must trigger after editing ( same on blur ).

if you need to trigger when user input something try to use about onkey "keydown" "keypress" "keyup" etc. . Example below !

$('.placementTest').each(function() {
            $(this).on('keypress',function (ev) {
                alert('done it');
            });
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
input 1 <input type="text" class="form-control placementTest" data-mask=''>
input 2 <input type="text" class="form-control placementTest" data-mask=''>
input 3 <input type="text" class="form-control placementTest" data-mask=''>

P.S. it's work fine !

Comments

0

Looks like you have two classes in the markup. Try to use one class

Comments

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.