Fun With Jquery
Fun With Jquery
jQuery
What is it?
A JavaScript library
Calling jQuery
Three requirements
1. Load jQuery library using a <script> tag 2. Decide how to call your code
Using document ready executes after page loads; e.g.,
$(document).ready(function() { your-code });
Using event handler setup from your document ready function, e.g.,
$(selector).click( your-code);
A Simple Example
Selectors
$(html-element-class)
Example: $(div)
$(#id)
Example: $(#txtFirstName)
$(.css-class)
Example: $(.divQuestion)
Selector Modifiers
Attribute modifier
$(selector[attribute=value]) only selects an element if it has the attribute set to value Example: $(input*type=text+)
Selecting by position
$(selector:position) match elements by position in wrapped set. E.g., :first, :last, :odd, :even, :eq(n)
$('#RecommendTx input[type=radio]')
Input elements of type radio following element with id of RecommendTx
$('#section1a .q1:checked')
Elements of class q1 that are checked following an element of class section1a
$('input:hidden[name="HistologyForm.ViewerStartTime"]')
Hidden input control with name = HistologyForm.ViewerStartTime
NOTE: There are almost always multiple ways to select same wrapped set
Events
Setting up event handlers
There are a number of ways to do it
Shortcuts
$(selector).click(handler) $(selector).blur(handler) $(selector).hover(handlerIn, handlerOut)
Methods
Once youve triggered your code (using an event) and selected your wrapped set (using a selector), its time to do something You can use the standard JavaScript DOM methods, but jQuery has some helpful methods
jQuery Methods
.toggle() toggles visibility; optionally using an animation and optional callback when animation is done
$("#q1").toggle(); $(#q1).toggle(true);
Equivalent to $(#q1).show();
$(#q1).toggle(false);
Equivalent to $(#q1).hide();
jQuery Methods
.attr(attribute) determines value of attribute
var bChecked = $(ctl).attr('checked')
jQuery Methods
.val() gets value of first element in wrapped set .val(value) sets value of all elements in wrapped set
jQuery Methods
.html() gets html contents of first element in wrapped set .html(htmlString) sets html of all elements in wrapped set
jQuery Methods
.css(attribute, value) gets/sets css attribute .addClass(className) adds class name to class attribute of wrapped set .removeClass(className) removes class name to class attribute of wrapped set .toggleClass(className) adds (if not there)/removes (if there) class name to class attribute of wrapped set
Another Example
function toggleMyChildren(ctl, bChecked) { var ctlRoot = $(ctl).parent();
// Toggle display $(ctlRoot).children('div').toggle(bChecked); $(ctlRoot).children('div').children().toggle(bChecked); }
Looping
$('div.question1 > input:checkbox:checked'). each(toggleChildrenUponLoad);
Debugging jQuery
Use browsers debugger (usually F12)
Dont bother with VS debugger But do add IntelliSense links in your .js files
Debugging tips
Check your selector: are you using . when you mean # or vice-versa? Are you treating a wrapped set as if its only one value?
You may need to use .first() or [0]