1

I am using Drupal 7 (quite new to both Drupal and Javascript / jQuery) and I got a class called "field-name-field-activity", now what I want is to put an onchange of every input in that class.

jQuery('.field-name-field-activity :input').onchange = function() {
    //enter code here
};

I am not sure if I'm using my onchange right here, I also saw some people using onChange instead of onchange, not sure what the difference is, but can anybody tell me how to use onchange the right way in my example?

Conceptual
  • 57
  • 2
  • 12

5 Answers5

4

With jquery just change will work:

$('.field-name-field-activity').change(function() {
  // your code here
});

Edit:

As the event is to be bind with text type, a better way is to use input event like:

$('.field-name-field-activity').on('input', function() {
    // your code here
});

The onchange event may not work correctly on some browsers in case of text fields.

Ankit Jaiswal
  • 22,859
  • 5
  • 41
  • 64
  • "As of jQuery 1.7, the `.on()` method is the preferred method for attaching event handlers to a document" - see [`.bind()`](http://api.jquery.com/bind/) documentation. – andyb May 03 '13 at 09:08
  • Oh yes, my sincere apologies! I completely misread the revision history. – andyb May 03 '13 at 09:09
2

This is a simple error of syntax try this jQuery('.field-name-field-activity :input').change(function() { //enter code here });

here is a link to the api reference http://api.jquery.com/change/

Nomad101
  • 1,688
  • 11
  • 16
  • I can only give one accepted answer, I'll give it to the person who answered first. (I'm sorry everyone else!) :( – Conceptual May 03 '13 at 09:02
1

Try like this:

jQuery('.field-name-field-activity input').change(function() {
    //enter code here
});

SIMPLE DEMO HERE

palaѕн
  • 72,112
  • 17
  • 116
  • 136
0

Try this -

jQuery('.field-name-field-activity').on('change',function() {
    //enter code here
});

http://api.jquery.com/change/

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
0

Concerning performance I would suggest not to use the ":input"-Selector.

Because :input is a jQuery extension and not part of the CSS specification, queries using >:input cannot take advantage of the performance boost provided by the native DOM >querySelectorAll() method.

Source: http://api.jquery.com/input-selector/

If the classname relates to input-Elements only anyway, just use the class-selector like Ankit Jaiswal suggested, because a single-class-selector performs best.

Source: Jquery element+class selector performance

Community
  • 1
  • 1
DonnyDee
  • 386
  • 2
  • 7