jQuery Topics

Full list of jQuery Snippets for Sublime 2

As I was learning the snippet shorthands for Sublime 2 I found it very useful to have this list of the main jQuery snippets so I could just flick through and learn them. So here is basically a dump of the best jQuery snippets in the awesome new text editor called Sublime 2. They are listed in order of relevance (in my opinion the most frequently used/useful ones first). Happy Scrolling!

Related posts:



jQuery Document Ready
Trigger: $.ready (tab)

version 1

$.jQuery(document).ready(function($) {
    // Stuff to do as soon as the DOM is ready. Use $() w/o colliding with other libs;
});

version 2

$.jQuery(document).ready(function() {
    // Stuff to do as soon as the DOM is ready;
});

version 3

$.$(document).ready(function() {
    // Stuff to do as soon as the DOM is ready;
});

jQuery Ajax
Trigger: $.ajax (tab)

$.ajax({
  url: '/path/to/file',
  type: 'POST',
  dataType: 'xml/html/script/json/jsonp',
  data: {param1: 'value1'},
  complete: function(xhr, textStatus) {
    //called when complete
  },
  success: function(data, textStatus, xhr) {
    //called when successful
  },
  error: function(xhr, textStatus, errorThrown) {
    //called when there is an error
  }
});

jQuery Each
Trigger: $.each (tab)

$.each(function(index) {
  this.innerHTML = this + " is the element, " + index + " is the position";
});

$.each(array/object, function(index, val) {
  //iterate through array or object
});

jQuery getJSON
Trigger: $.getJSON (tab)

$.getJSON('/path/to/file', {param1: 'value1'}, function(json, textStatus) {
  //optional stuff to do after success
});

jQuery getScript
Trigger: $.getScript (tab)

$.getScript('path/to/file', function(data, textStatus) {
  //optional stuff to do after getScript
});

Script Include
Trigger script (tab)

<script src="/javascripts/application.js" type="text/javascript" charset="utf-8" async defer></script>

jQuery Animate
Trigger: $.animate (tab)

version 1

$.animate({param1: value1, param2: value2}, speed, function() {
  // stuff to do after animation is complete
})

version 2

$.animate({param1: value1, param2: value2}, speed)

jQuery DOM Attr Property
Trigger: .attr (tab)

version 1

.attr({
  attribute1: 'value1',
  attribute2: 'value2'
})

version 2

.attr('attribute', 'value')

jQuery Map
Trigger: $.map (tab)

version 1

$.map(function(index, elem) {
  return something;
})

version 2

$.map(array, function(item, index) {
  return something;
});

jQuery CSS
Trigger: .css (tab)

Version 1

.css({
  property1: 'value1',
  property2: 'value2'
})

Version 2

.css('property', 'value')

jQuery Bind
Trigger: .bind (tab)

.bind('event name', eventData, function(event) {
  // Act on the event
});

jQuery Live
Trigger: .live (tab)

.live('event type(s)', function(event) {
    // Act on the event
});

jQuery Plugin
Trigger: plugin (tab)

Version 1

jQuery.fn.myeffect = function(speed, easing, callback) {
  return this.animate({param1: 'value'}, speed, easing, callback);
};

Version 2

(function($) {
  $.extend($.expr[':'], {
    selectorName: function(element, index, matches, set) {

      return something;
    }
  });
})(jQuery);

Version 3 <-- awesome!

(function($) {
// What does the pluginName plugin do?
$.fn.pluginName = function(options) {

  if (!this.length) { return this; }

  var opts = $.extend(true, {}, $.fn.pluginName.defaults, options);

  this.each(function() {
    var $this = $(this);

  });

  return this;
};

// default options
$.fn.pluginName.defaults = {
  defaultOne: true,
  defaultTwo: false,
  defaultThree: 'yay!'
};
})(jQuery);

Key Value Pair
Trigger : (tab)

key: "value",

jQuery Wrap
Trigger: .wrap (tab)

Version 1

.wrap('<div class="extra-wrapper"></div>')

Version 2

.wrapAll('<div class="extra-wrapper"></div>')

Version 3

.wrapInner('<div class="extra-wrapper"></div>')

jQuery Toggle
Trigger: .toggle (tab)

Version 1

.toggle('slow/400/fast')

Version 2

.toggle(function() {
    // Stuff to do every *odd* time the element is clicked;
}, function() {
    // Stuff to do every *even* time the element is clicked;
});

jQuery Trigger
Trigger: .trigger (tab)

Version 1

.trigger('event name')

Version 2

.triggerHandler('event name')

These are the main ones but there are like 30-40 more snippets which are not mentioned here that can be found in the full List of jQuery Snippets on GitHub: https://github.com/kswedberg/jquery-tmbundle/tree/master/Snippets.

Other Snippets I found Useful:

HTML
Trigger: html (tab)

<html>
<head>
    <title></title>
</head>
<body>

</body>

</html>

HTML 4 Doctype Transitional
Trigger: html:4t (tab)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title></title>
</head>
<body>

</body>
</html>

XHTML 1 Doctype Transitional
Trigger: html:xt (tab)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
    <title></title>
</head>
<body>

</body>
</html>

Read the full/original article at jQuery4u

    Related Posts

    Comments are closed.

    jQuery Resources

    Learn more about jQuery

    jQuery News & Links

    I only just found out that the jQuery validation plugins has a validation rule called “remote” which can be used ...

    jQuery function to check if horizontal scroll is present – hasHScrollBar() – (or vertical check below also, util function to ...

    jQuery function to Set any DOM Element to Top View (bring to front) using CSS Z-Index property.

    Is it possible to declare arrays in JavaScript object literal notation? Example 1 – this works with arrays Declaration: Storage ...

    To get the a variables type using jQuery there is a jQuery function called .type() which returns “array”, “string”, “number”, ...

    In this post you can find tutorials which explain step by step different API use cases with jQuery like Google ...

    For valuable work on creation of sites you need a good comfortable editor necessarily. There are many requiring paid products ...

    Today we are sharing with you a collection of awesome jQuery Camera Photo plugins. They offer a range of image ...

    Back in the day, if you saw something that was animated on a website it was automatically assumed to be ...

    In this post, we have compiled a list of 10 jQuery HTML5 Audio players available today, most allow native audio ...

    jQuery Mobile is a powerful framework for making mobile web applications. But can we use it to convert existing desktop ...

    I have jotted a quick post on a Basic JavaScript Regular Expression Example to give beginners out there a taste ...

    In April 2012′s edition of Interestingly Random JavaScript, jQuery and Web Development we bring you some very cool stuff such ...

    In this post we are sharing you a roundup of 10 really jQuery plugins as of today. Pretty cool plugins ...

    As always expected from the jQuery community, the compilation we have for you today are some good and impressive recently ...

    A collection of jQuery PNG/JPEG/GIF plugins that enables image animation, cartoon-like background, etc… let them help you design displays for ...

    A collection of JavaScript/jQuery Zip/File/Compressor plugins that allows you to minify your JS code and compress your JS files ready ...

    Quick jQuery code snippet on how to keep an element in view. For demo scroll down on any page on ...

    Here is what I think is the best and most reliable way to load jQuery library and jQuery UI Libraries. ...

    jQuery code snippet which outputs all attributes of element using the default .attr() function on any dom element(s). usage: output:

    More Links & News

    Community Resources

    Use jQuery and the Google Maps API to create your own map application.

    Super cool and easy way to get stylish tooltips with jQuery.

    The right way to include the jQuery library in WordPress.

    Share a resource