0

I have a variable that is retrieved via a .click function and I want to pass it to another function but I"m having trouble passing it correctly because when I call the other function nothing happens.

var className;
var flavor;
(function ($) {

  Drupal.myTest = Drupal.myTest|| {};

  Drupal.behaviors.myTest= {

    attach: function (context, settings) {

          Drupal.myTest.myFunction= function (className) {
            var passedVar= className;
            alert(passedVar);
          };



          $('.buttons').on('click', function(event) {
            className = $(this).attr('class');
          });

          $('#test').on('click, function(event) {
        Drupal.myTest.myFunction();
          });

    }

  };

}(jQuery));
0

2 Answers 2

1

Try

(function ($) {
    var className;
    var flavor;

    Drupal.myTest = Drupal.myTest|| {};

    Drupal.behaviors.myTest = {
        attach: function (context, settings) {
            Drupal.myTest.myFunction= function (className) {
                var passedVar= className;
                alert(passedVar);
            };

            $('.buttons').on('click', function(event) {
                className = $(this).attr('class');
            });

            $('#test').on('click', function(event) {
                Drupal.myTest.myFunction(className);
            });
        }
    };

}(jQuery));
Sign up to request clarification or add additional context in comments.

5 Comments

This didn't seem to work. Could you please see my edit to my question above and you may be able to get a better representation of my code?
@CR47 you need to pass className as a parameter in the call Drupal.myTest.myFunction(className);
@CR47 that is a parameter definition... not there... where you call that function... in this case inside $('#test').on('click', function(event) { Drupal.myTest.myFunction(className); });
@CR47 @CR47 in your case myFunction is accepting a parameter with name className so in the scope of the function className will refer the local parameter not the closure variable className
@CR47 I've updated the answer with your new code... another solution is to remove the parameter className from myFunction so that inside the function scope className will refer to the closure variable
0

Declare the function outside of $(document).ready()

<script type="text/javascript">
    var myPassedVar;

    $(document).ready(function() {
        $('.buttons).click(function() {
          var className = $(this).attr(class);
        });

        myFunction = function (className) {
          myPassedVar = className;
          alert(myPassedVar);
        };

        $('#test').click(function() {
          myFunction(className);
        }
    }); //END $(document).ready()
</script>

3 Comments

Did you mean declare the variable?
OUTSIDE of document ready. Declare it, create it, assign it a value, just make sure it exists outside $(document).ready() -- or its shorthand version: $(function()
I made an edit to my question that shows my code better in addition to the suggestion you made.

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.