0

I have a form (with class input) with lots of input tags. I want to write onchange event for the input[type="text"] and input[type="number"]

How to do it?

All i have now is:

$('.input input[type="number"],.input input[type="text"] ').change() ...

There has to be a shorter way of doing this

4
  • "there has to be a shorter way of doing this" - why ...? Commented Jun 27, 2013 at 12:04
  • 2
    set all the inputs that you want an onchange event for to have the same class, and just use the class name Commented Jun 27, 2013 at 12:04
  • @CBroe is what i have mentioned, the ONLY way of doing this? Commented Jun 27, 2013 at 12:05
  • by the way, I don't see the point in giving all your input tags an input class... Commented Jun 27, 2013 at 12:12

3 Answers 3

3

Use a common class for each input you want to use for the event.

<input class="changingInput" type="text">
<input class="changingInput" type="number">

$('.changingInput').change()

you can also limit it to only inputs with that class in case you also have the class on things that arent inputs

$('input.changingInput').change()
Sign up to request clarification or add additional context in comments.

Comments

0

You can

$('.input').find('input[type="number"], input[type="text"] ').change()

Another solution is to add a commons class for all the desired elements and access it via the class selector like $('.my-inputs')

Comments

0

you can make it shorter by simple give them some attribute that can be used in every input like using the same class name(see Patrick answer) or give them attribute like

<input changeattribute="SomeValue" type="text">
<input changeattribute="SomeValue" type="number">
$('input[changeattribute="SomeValue"]').change()..

Comments

Your Answer

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