Prototype Jquery: To and From Javascript Libraries
Prototype Jquery: To and From Javascript Libraries
jQuery
$('#speech1').show();
CSS Based Selectors
• Prototype - $$
To narrow down it's context use
Element.getElementsBySelector(selector)
(or Element.select(selector) in 1.6)
• jQuery - $
Virtually all of jQuery's DOM selection is
done using CSS 1-3
Selector Examples
Prototype
$$('.dialog').invoke(‘show’);
$('final-speech').getElementsBySelector ↩
('DIV.final-dialog').each(Element.hide);
// 1.6
$('final-speech').select('DIV.final- ↩
dialog').invoke('hide');
Selector Examples
jQuery
$('.dialog').show();
$('#final-speech DIV.final-dialog') ↩
.hide();
DOM Ready Event
jQuery
$(document).ready(function(){}); // or
$(function(){});
Iteration
• Prototype - current active element, and
position is passed in to callback function.
[el1, el2].each(fn(el, i))
• Prototype - addClassName,
removeClassName, toggleClassName,
hasClassName
• jQuery - addClass, removeClass,
toggleClass, is (for class matching)
Events
• Prototype - Event.stop()
• jQuery - return false or
event.stopPropagation() (event is passed in
to the callback)
Ajax
Prototype
new Ajax.Request(url[, options])
jQuery
Prototype jQuery
onCreate beforeSend
onSuccess success
onException error
onComplete complete
Ajax Examples
Prototype
new Ajax.Request(‘/profile’, {
method: ‘post’,
parameters:$H({‘action’:‘check_username’,
‘username’:$F(‘username’)}),
onSuccess: function (j) {
// do stuff with response
}
});
Ajax Examples
jQuery
$.ajax({ url: '/profile',
data: {'action':'check_username',
'username': $('#username').val()},
type: 'post',
success: function (json) {
// do stuff with response
}
});
Plugins / Extensions
Prototype
Element.addMethods({myPlugin : function ↩
(element, args) { return element; }});
jQuery
jQuery.fn.myPlugin = function (args) ↩
{ return this; };
Browser Detection