Javascript & jQuery Cheat Sheet

Even the smartest cookies forget things from time to time..
Here’s a collection of Javascript & jQuery code snippets for your reference. Enjoy 🙂

Run jQuery code when page has finished loading (DOM ready)

$(document).ready(function() {
	// code here
});
 
// jQuery shortcut to DOM ready
$(function() {
	// code here
});

Run jQuery in “no conflict” mode

jQuery.noConflict();
 
// run code when DOM ready using $ shortcut
jQuery(document).ready(function($) {
	// code here
});

Get selected text from select element

$("#ID :selected").text()

Get multiple selected text from select element into array

var arr_text = [];
$("#ID :selected").each(function(i, selected){
	arr_text[i] = $(selected).text();
});

Bind a click event

$("#ID").bind("click", function(){
	alert("Clickity Click");
});

Load jQuery from Google’s CDN

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js'></script>

Comments are closed.