0

How do I call a custom defined function from within a jQuery click event?

I define a clearOrder function that executes some actions. Then, when I click on a DOM element, I want to call that function, and then do some more actions.

The actions are simply applying and removing classes. As you can see I want to clear the specified existing classes from elements as a sort of reset before adding specific classes to the DOM elements.

html

<div id="neti" class="tile"> contents </div>
<div id="sife" class="tile"> contents </div>

javascript

(function() {
  clearOrder(function() {
    return $('.tile').removeClass("order-1 order-2 order-3 order-4 order-5 order-6 order-7 order-8 order-9 order-10 order-11 order-12 order-13 order-14 order-15 order-16");
  });

  jQuery(function() {
    $('#neti').click(function() {
      clearOrder.call;
      $('this').addClass("active");
      $('#neti').addClass("order-1");
      $('#sife').addClass("order-2");

    });
  });

}).call(this);

The script behavior should be that when I click on an element, it should remove all of the specified classes, then apply certain classes.


edit: changed to the following, but still not functioning.

(function() {
  clearOrder(function() {
    return $('.tile').removeClass("order-1 order-2 order-3 order-4 order-5 order-6 order-7 order-8 order-9 order-10 order-11 order-12 order-13 order-14 order-15 order-16");
  });

  jQuery(function() {
    $('#neti').click(function() {
      clearOrder();
      $('this').addClass("active");
      $('#neti').addClass("order-1");
      $('#sife').addClass("order-2");
6
  • 2
    just add () at the end of the function name to invoke it.. like clearOrder();... there is no need to use .call Commented May 30, 2014 at 4:49
  • I tried your suggestion. It does not seem to function properly still. Commented May 30, 2014 at 4:53
  • any error in your browser console Commented May 30, 2014 at 4:56
  • no, it loads, but just doesn't behave properly: there is no visible adding or removing of any classes in the inspector. Commented May 30, 2014 at 4:59
  • Currently you're not defining clearOrder. Can you display where it's defined? (I'm thinking maybe it's scoped so your anonymous function can't get to it, which would cause it to error out). Commented May 30, 2014 at 5:00

1 Answer 1

1

There are multiple problems

(function () {
    //function declaration syntax was wrong
    function clearOrder() {
        return $('.tile').removeClass("order-1 order-2 order-3 order-4 order-5 order-6 order-7 order-8 order-9 order-10 order-11 order-12 order-13 order-14 order-15 order-16");
    };

    jQuery(function ($) {
        $('#neti').click(function () {
            //add () to the end of the function reference to invoke it
            clearOrder();
            // this is not a string literal, it has to be used as a keyword(without enclosing '') to refer the current clicked element
            $(this).addClass("active");
            $('#neti').addClass("order-1");
            $('#sife').addClass("order-2");

        });
    });

}).call(this);

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

3 Comments

where/why does the $ in jQuery(function ($) { come from?
jQuery passes an instance of self to the dom ready handler method, so that we can refer to the jQuery using any variable name that we like...
thank you so much!!! Using this tool, I was able to figure out the coffee as well. haml-html-coffeecup-javascript-coffeescript-converter.smullinde…

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.