Some basic jQuery examples I created over time to help me learn jQuery. Hopefully you find this helpful as well! I apologize as some of these examples are a work in progress.
Quick Reference:
$(document).ready(function(){
alert ('Ready to do your bidding, master');
});
$(function() { alert('Good to go'); });
Quick Selection
$(document).ready(function(){
$('p').show(); // Select all the p elements
$('p,div,h2,input').hide(); // Select multiple elements
$('#someId tr') // Select table rows within id someId
Filters
$('li:eq(2)') // Select the second li within list items
Content Filters
$('li:contains(second-level)') // Selects li that contains the text "second level"
Examples:
$("p").addClass("myClass yourClass"); // Adds more than one class to all p elements
});
* When the document is ready run our function. Almost everything you do in jQuery needs to be done after the document is ready.
Selecting Based on attribute
Click the buttons below to select different elements based on their attributes.
Show/Hide a rows based on checked or unchecked form checkbox. Two different Examples
Form: Example Table 1:
Manufacturer row One here
Manufacturer row Two here
$(document).ready(function(){
$('input.manOne').change(function () {
if ($(this).is(":checked")) {
//do the stuff that you would do when 'checked'
$('tr.manOne').hide();
return;
}
//Here do the stuff you want to do when 'unchecked'
$('tr.manOne').show();
});
});
Form:
Example Table 2:
Manufacturer row One here
Manufacturer row Two here
Manufacturer row Three here
$(document).ready(function(){
$('#distributorAnchors input').change(function () {
var cls = $(this).attr("class");
if ($(this).is(":checked")) {
//do the stuff that you would do when 'checked'
$('tr.' + cls).hide();
return;
}
//Here do the stuff you want to do when 'unchecked'
$('tr.' + cls).show();
});
});
This
Example of the 'this' selector being used to select an image within the class or id you have specified. http://api.jquery.com/toggle-event/
$(".face").toggle(
function () {
$('img',this).attr('src','http://www.bhtuning.com/images/smilies/icon_sad.gif');
},
function () {
$('img',this).attr('src','http://cdn7.droidmill.com/media/market-media/com.faflabs.make_me_happy_icon.png');
});
Sliding Panel
Used .click method to capture the event and then the .slideToggle method to slide the div into view matching the height of the element. CSS used to hide the div id panel until button event (click) occurs.
.slideToggle( [ duration ], [ callback ] ) - Allows for a callback function that calls once the animation is complete
Accordian
With the click method attached to the H3 Title tags of each section we use the .slideToggle method to display the content. The .eq method is used to specify a specific element within a set of matched elements. In thise case we chose .eq(2). At the same time the .siblings() method is used to get the siblings or the p element and apply the .slideup method which hides the matching elements with a sliding motion.
Title Here
The contents of your accordian can go here. Graphics, text, more elements or whatever you like can be placed in here. Try it for yourself!
Title Here
The contents of your accordian can go here. Graphics, text, more elements or whatever you like can be placed in here. Try it for yourself!
Title Here
The contents of your accordian can go here. Graphics, text, more elements or whatever you like can be placed in here. Try it for yourself!
Title Here
The contents of your accordian can go here. Graphics, text, more elements or whatever you like can be placed in here. Try it for yourself!
Animate Hide method
We use the .click method to fire when the link is clicked. We then use the .parents method which grabs the ancestor/div of the a element. We then use the .animate method to hide the div slowly with an opacity change as it gets hidden. return false at the end to prevent the default link action.
Chain Gang
We are chaining multiple methods here. Watch speedy zip around the screen.
Event Click Handler: (Hides then shows again, threw on a blue border for fun. Also displays url of link clicked) Example Link
$(".link-notify").click(function(event){
alert("You just clicked " + this.href);
event.preventDefault();
.hide("slow").show("slow");
$(this).hide("slow").show("slow").addClass("setBold");
});
$("#overlay-test").click( function() {
var position = $(".basic-info").offset(); // Grabs the position
$("#populate-overlay").css( { position: "absolute", left: position.left, top: position.top } ).show();
} );
$(".close-x").click(function () {
$(this).parent().hide();
});
$(".cancel-overlay").click(function () {
$(this).parent().hide();
});
Highlight and paragraph change
This is an example paragraph one.
This is an example paragraph two.
This is an example paragraph three.
This is an example paragraph four.
$("p.highlight").click(function () {
$(this).toggleClass("highlight").html("I'm am now a funky light switch now! Click me on and off.");
});
$(".paragraph-example").click(function() {
var str = $(".paragraph-example p:first").text();
$(".paragraph-example p:last").html(str).addClass("highlight");
});
.Each Method
Description: Iterate over a jQuery object, executing a function for each matched element. It's designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element. In this example we iterate through alerting the index and text contained within each li. We also use .css method to apply the color red to each li. (Source api.jquery.com)
AJAX - Communication between client and server
The server can dynamically shape data based on input from the user/browser/client. Using the .click method the user selects from the list of links. Using the .load method, data (test.php) is loaded from the server and returned into the matched element (id - ajax-list1). We use the .text method to get the contents of the element (in this case the a link).
Validate plugin is called via one line - $("#shareInfo").validate(); which is the ID on the form.
I added the class="required" to each of the input/text area elements that needs to be validated. Added class "email" to have it validate for e-mail. Additional validation can be added for things such as min max.
Overlay is created using CSS/absolute positioning and jquery to grab the position relative to the element position - $("#shareForm").css( { position: "absolute", left: position.left, top: position.top } )
Both buttons populate the same overlay.
Basic jQuery form using validate plugin and a character count.
Please fill in the form below and Send to share this information.
How to immitate something being clicked. Especially useful if you have a function tied to a click that you want to trigger. The following selects the first element on the page with class distiLabel.
$(".distiLabel:first").click();
Selects the parent div of "this" div then the next div to the parent div. Then hides "this" which is the link itself. Then it hides its parent.