1

I have a function in jQuery

  jQuery(function () {
      jQuery(".one").showcase({
          animation: { type: "fade" },
          titleBar: { autoHide: false },
          navigator: { autoHide: true }
      });
      prettyPrint();
      jQuery(".two").showcase({
          animation: { type: "fade" },
          titleBar: { autoHide: false },
          navigator: { autoHide: true }
      });
      prettyPrint();
      jQuery(".three").showcase({
          animation: { type: "fade" },
          titleBar: { autoHide: false },
          navigator: { autoHide: true }
      });
      prettyPrint();
  });

now note the code above. i repeat the showcase function three times. I want create the function once by array. how can it possible. I saw the jQuery array example, but cant understand.

3 Answers 3

4

The best way wouldn't be to use an array, but to alter the jQuery selector to target all 3 at once;

  jQuery(".one,.two,.three").showcase({
      animation: { type: "fade" },
      titleBar: { autoHide: false },
      navigator: { autoHide: true }
  });
  prettyPrint();

... which uses the multiple selector, although you might want to add another class to those elements showcase(?) and call it like;

  jQuery(".showcase").showcase({
      animation: { type: "fade" },
      titleBar: { autoHide: false },
      navigator: { autoHide: true }
  });
  prettyPrint();

... instead.

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

1 Comment

if there is a number of class,then it is not better to write like that
2

You can do:

  jQuery(function () {
      var arObj = ['.one','.two','.three'];

      for(var i = 0; i< arObj.length ; i++){
          jQuery(arObj[i]).showcase({
              animation: { type: "fade" },
              titleBar: { autoHide: false },
              navigator: { autoHide: true }
          });
          prettyPrint();
      }
  });

Comments

1

If you want an array-based solution, you can do something like:

jQuery.each([".one", ".two", ".three"], function(index, value) {
    jQuery(value).showcase({
        animation: { type: "fade" },
        titleBar: { autoHide: false },
        navigator: { autoHide: true }
    });
    prettyPrint();
});

...or:

function fadeAll(arr) {
    jQuery.each(arr, function(index, value) {
        jQuery(value).showcase({
            animation: { type: "fade" },
            titleBar: { autoHide: false },
            navigator: { autoHide: true }
        });
        prettyPrint();
    });
}

Otherwise just go with Matt's answer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.