1

I have a simple form which currently uses jQuery to do some dynamic calculations based on user form input.

Most form fields are disabled and their value is calculated using the values of the non-disabled fields. Part of the calculation involves an AJAX call to save the field value to a database.

I could use the following code:

$('#specific-field').change(function() {
    calculateThings();
});

But this would involve repeating for every field whose value can be altered.

I could also do this:

$('form').change(function() {
    calculateThings();
});

But then I'd have to save and update every database field every time a change is made, when only one field at a time will potentially contain a different value.

Is there a way to merge the two possibilities, where I can monitor changes at the form level but then detect which specific field within the form was changed?

Roy
  • 705
  • 2
  • 11
  • 32

7 Answers7

2

Couldn't you do something like:

$('.genericFieldClass').change(function() {
    calculateThings();
});
Remko
  • 968
  • 6
  • 19
1

You could do the following:

$("select").change(function () { 
    var changedSelectId = $(this).attr("id");
});
David MacCrimmon
  • 966
  • 1
  • 6
  • 19
0

You could get all the input fields in the form by using:

$('form input').change(function() {
    calculateThings();
});

And if you have a select just add form select in there:

$('form input, form select').change(function() {
    calculateThings();
});
putvande
  • 15,068
  • 3
  • 34
  • 50
0

You can't do it because the change event does not propagate.

You can add a class like changeinput to all the input elements which can be changed then

$('.changeinput').change(function() {
    calculateThings();
});
Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Classes were designed to allow multiple elements to have the same functionality applied to them. With that in mind try this:

$('.dynamic-field').change(function() {
    calculateThings();
});

You can then give the elements which should trigger the calculation the class 'dynamic-field' and you're done.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

would this help?

$('input,select,textarea').change(function(){
   var elementChanged = $(this).attr("id");
   //ajax call you wanna do and whatever else
});
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
pythonian29033
  • 5,148
  • 5
  • 31
  • 56
0

Add a class to the form elements or use the child selector if you know the type of inputs you want to watch out for. If you use a class:

<input type="" class="myClass" />
<input type="" class="myClass" />
<input type="" class="myClass" />
<input type="" class="myClass" />

In the jquery:

$('.myClass').change(function() {
   calculateThings();
});

Or without using class:

$('form').children("input[type='text']").each(function(){             // Or whatever input type you want to look out for
    $(this).change(function(){
       calculatethings();
    });
});
reggaemahn
  • 6,272
  • 6
  • 34
  • 59