238 Mini-API / Cheat Sheet You May Detach These Sheets Selecting
238 Mini-API / Cheat Sheet You May Detach These Sheets Selecting
Selecting:
select based on a selector: $(selectorIdentifier)
select based on an ID: $('#idIdentifier')
select based on a class name: $('.classIdentifier')
Wildcards:
^abc selects items that begin with ‘abc’
Example: $('img[src^="ball"]') will select images with an src that
begins with the letters ‘ball’
*abc selects items that contain ‘abc’
Example: $('img[src*="ball"]') will select images with an src that
contains with the letters ‘ball’
$abc selects items that end with ‘abc’
Example: $('img[src$="ball"]') will select images with an src that
ends with the letters ‘ball’
Conditionals:
Logical AND uses ‘&&’ e.g. if (name==’Bob’ && color==’blue’)
Logical OR uses ‘||’ e.g. if (name==’Bob’ || name==’Lisa’)
1
toggleClass( class name ) – Adds/Removes ‘className’ from the selector
depending on whether or not it is currently applied
Math.round(Number) – Returns ‘Number’ rounded to the nearest numeric value
attr( attributeName ) – Returns the current value of ‘attributeName’
attr( attributeName, value ) - Sets the attribute ‘attributeName’ to ‘value’
removeAttr( attributeName ) – Removes attribute ‘attributeName’
each( named or anonymousFunction ) – When applied to a selector, the function
will be applied to every selected item
focus(named or anonymousFunction) - Function is executed when selected element
gains focus
blur(named or anonymousFunction) - Function is executed when selected element
loses focus
click( named or anonymousFunction ) – Function is executed when selected
element is clicked
val() – Retrieves the value of the selected element
prop("checked") - Returns ‘true’ if the selected element has been checked
event.preventDefault(evt) - Default action of the event will not be triggered
Example of each() function that makes use of an anonymous function and the ‘this’ construct:
$('img').each( function() {
var currentImageName = $(this).attr('src');
alert(currentImageName);
});
2
Example of a for-loop:
for (var counter=0; counter<10; counter+=1)
{
//body of loop goes here
}
3
Some Regular Expression Patterns