Applied this JS http://jsfiddle.net/tbq8eo8b/10/ to webforms. As you can see in fiddle, it calculates total amount. When in Drupal, it gives 0 no matter what amounts you select, it always prints 0. What's wrong?

Drupal code:

(function ($) {
Drupal.behaviors.bartik = {
attach: function(context, settings) {

/*Add your js code here*/
    var $selects = $("select").change(function (e) {
        var total = 0;
        $selects.each(function() {
            var val = this.value.match(/\$(\d+)/);
            total += val ? +val[1] : 0;



Also console prints:

TypeError: $ is not a function
$("select").change(function ()
  • First thing, it should be $("select", context) as your top-level selector. Second thing, are you sure it is run after form is ready and prepared? Third thing: How you debugged it? Does it find selects at all? Are select's values in the format you expect them to be? and so on...
    – Mołot
    Sep 24, 2014 at 8:39
  • 2
    Try changing Drupal.behaviors.bartik with Drupal.behaviors.something_unique. You sure there is an input field with total class? Also, you will need to use more precise selectors.
    – AKS
    Sep 24, 2014 at 8:47
  • I changed it, but same. Under input.total it still prints 0. Sep 24, 2014 at 8:56
  • What about questions from my comment? Have you tested these things?
    – Mołot
    Sep 24, 2014 at 8:57
  • I applied the context part. Still same. It prints 0 in the input that has class .total Also updated question with Console errors. Thanks for helping. Sep 24, 2014 at 8:58

1 Answer 1


should be


in your last line of code.

  • Yes, I already fixed that, didn't change anything though. Sep 24, 2014 at 9:18
  • @OptimusPrime At least it will remove the console error $ is not a function and your script will be executed at all. Please don't chameleon your question now.
    – Mołot
    Sep 24, 2014 at 9:20
  • No, error is still there and same. Sep 24, 2014 at 9:21
  • @OptimusPrime are you sure your browser refreshed js files?
    – Mołot
    Sep 24, 2014 at 9:22
  • I'm sorry. You were right, the errors are gone, But it still prints zeros, but in the jsfiddle the script works fine. What is Drupal preventing from working it right? Why it still prints zeros instead of total amount? Thanks for helping. Sep 24, 2014 at 9:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.