SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    N.Ireland
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS shorthand for multiple functions

    Hi,

    I'm currently doing this
    Code:
    $(document).ready(function(){
      $('.slider1').bxSlider({
        slideWidth: 300,
        minSlides: 2,
        maxSlides: 3,
        slideMargin: 10
      });
    });
    </script>
    <script>
    $(document).ready(function(){
      $('.slider2').bxSlider({
        slideWidth: 300,
        minSlides: 2,
        maxSlides: 3,
        slideMargin: 10
      });
    });
    Surely I can shorten it like;

    Code:
    $(document).ready(function(){
      $('.slider1','.slider2').bxSlider({
        slideWidth: 300,
        minSlides: 2,
        maxSlides: 3,
        slideMargin: 10
      });
    });
    </script>
    Enjoy what you do, enjoy what you learn but above all ENJOY IT!
    http://scbdesign.site11.com

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,890
    Mentioned
    211 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by stephen_ View Post
    Surely I can shorten it like ...
    Indeed you can, although this:

    Code JavaScript:
    $('.slider1','.slider2').bxSlider({

    should be:

    Code JavaScript:
    $('.slider1, .slider2').bxSlider({

    See: http://api.jquery.com/multiple-selector/

  3. #3
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You were very close, instead of comma separating the strings themselves you simply add a comma inside the first string itself hence the following:

    Code:
    '.slider1','.slider2'
    will become.
    Code:
    '.slider1, .slider2'
    Last edited by chris.upjohn; Jun 4, 2013 at 04:48. Reason: Pullo just beat me

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    N.Ireland
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks both - Pullo thanks for the link apologies for being lazy I should have looked at this beforehand.

    Chris.upjohn - my best mate is called Chris John, what's the chances?
    Enjoy what you do, enjoy what you learn but above all ENJOY IT!
    http://scbdesign.site11.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •