jQuery effects
The basic effects described here do not require any additional files; everything you need is already included in web2py_ajax.html.
HTML/XHTML objects can be identified by their type (for example a DIV), their classes, or their id. For example:
<div class="one" id="a">Hello</div>
<div class="two" id="b">World</div>
They belong to class “one” and “two” respectively. They have ids equal to “a” and “b” respectively.
In jQuery you can refer to the former with the following CSS-like equivalent notations
jQuery('.one') // address object by class "one"
jQuery('#a') // address object by id "a"
jQuery('DIV.one') // address by object of type "DIV" with class "one"
jQuery('DIV #a') // address by object of type "DIV" with id "a"
and to the latter with
jQuery('.two')
jQuery('#b')
jQuery('DIV.two')
jQuery('DIV #b')
or you can refer to both with
jQuery('DIV')
Tag objects are associated to events, such as “onclick”. jQuery allows linking these events to effects, for example “slideToggle”:
<div class="one" id="a" onclick="jQuery('.two').slideToggle()">Hello</div>
<div class="two" id="b">World</div>
Now if you click on “Hello”, “World” disappears. If you click again, “World” reappears. You can make a tag hidden by default by giving it a hidden class:
<div class="one" id="a" onclick="jQuery('.two').slideToggle()">Hello</div>
<div class="two hidden" id="b">World</div>
You can also link actions to events outside the tag itself. The previous code can be rewritten as follows:
<div class="one" id="a">Hello</div>
<div class="two" id="b">World</div>
<script>
jQuery('.one').click(function(){jQuery('.two').slideToggle()});
</script>
Effects return the calling object, so they can be chained.
When the click
sets the callback function to be called on click. Similarly for change
, keyup
, keydown
, mouseover
, etc.
A common situation is the need to execute some JavaScript code only after the entire document has been loaded. This is usually done by the onload
attribute of BODY but jQuery provides an alternative way that does not require editing the layout:
<div class="one" id="a">Hello</div>
<div class="two" id="b">World</div>
<script>
jQuery(document).ready(function(){
jQuery('.one').click(function(){jQuery('.two').slideToggle()});
});
</script>
The body of the unnamed function is executed only when the document is ready, after it has been fully loaded.
Here is a list of useful event names:
Form events
onchange
: Script to be run when the element changesonsubmit
: Script to be run when the form is submittedonreset
: Script to be run when the form is resetonselect
: Script to be run when the element is selectedonblur
: Script to be run when the element loses focusonfocus
: Script to be run when the element gets focus
Keyboard events
onkeydown
: Script to be run when key is pressedonkeypress
: Script to be run when key is pressed and releasedonkeyup
: Script to be run when key is released
Mouse events
onclick
: Script to be run on a mouse clickondblclick
: Script to be run on a mouse double-clickonmousedown
: Script to be run when mouse button is pressedonmousemove
: Script to be run when mouse pointer movesonmouseout
: Script to be run when mouse pointer moves out of an elementonmouseover
: Script to be run when mouse pointer moves over an elementonmouseup
: Script to be run when mouse button is released
Here is a list of useful effects defined by jQuery:
Effects
jQuery(...).show()
: Makes the object visiblejQuery(...).hide()
: Makes the object hiddenjQuery(...).slideToggle(speed, callback)
: Makes the object slide up or downjQuery(...).slideUp(speed, callback)
: Makes the object slide upjQuery(...).slideDown(speed, callback)
: Makes the object slide downjQuery(...).fadeIn(speed, callback)
: Makes the object fade injQuery(...).fadeOut(speed, callback)
: Makes the object fade out
The speed argument is usually “slow”, “fast” or omitted (the default). The callback is an optional function that is called when the effect is completed.
jQuery effects can also easily be embedded in helpers, for example, in a view:
{{=DIV('click me!', _onclick="jQuery(this).fadeOut()")}}
Other useful methods and attributes for handling selected elements
Methods and attributes
jQuery(...).prop(name)
: Returns the name of the attribute valuejQuery(...).prop(name, value)
: Sets the attribute name to valuejQuery(...).html()
: Without arguments, it returns the inner html from the selected elements, it accepts a string as argument for replacing the tag content.jQuery(...).text()
: Without arguments, it returns the inner text of the selected element (without tags), if a string is passed as argument, it replaces the inner text with the new data.jQuery(...).val()
: Without arguments, it returns the current value of the first element of the selected elements, if a string is passed as argument, it replaces the value of every matched element.jQuery(...).css(name, value)
: With one parameter, it returns the CSS value of the style attribute specified for the selected elements. With two parameters, it sets a new value for the specified CSS attribute.jQuery(...).each(function)
: It loops trought the selected elements set and calls function with each item as argument.jQuery(...).index()
: Without arguments, it returns the index value for the first element selected related to its siblings. (i.e, the index of a LI element). If an element is passed as argument, it returns the element position related to the selected elements set.jQuery(...).length
: This attribute returns the number of elements selected.
jQuery is a very compact and concise Ajax library; therefore web2py does not need an additional abstraction layer on top of jQuery (except for the ajax
function discussed below). The jQuery APIs are accessible and readily available in their native form when needed.
Consult the documentation for more information about these effects and other jQuery APIs.
The jQuery library can also be extended using plugins and User Interface Widgets. This topic is not covered here; see ref.[jquery-ui] for details.
Conditional fields in forms
A typical application of jQuery effects is a form that changes its appearance based on the value of its fields.
This is easy in web2py because the SQLFORM helper generates forms that are “CSS friendly”. The form contains a table with rows. Each row contains a label, an input field, and an optional third column. The items have ids derived strictly from the name of the table and names of the fields.
The convention is that every INPUT field has an id tablename_fieldname
and is contained in a row with id tablename_fieldname__row
.
As an example, create an input form that asks for a taxpayer’s name and for the name of the taxpayer’s spouse, but only if he/she is married.
Create a test application with the following model:
db = DAL('sqlite://storage.sqlite')
db.define_table('taxpayer',
Field('spouse_name'))
the following “default.py” controller:
def index():
form = SQLFORM(db.taxpayer)
if form.process().accepted:
response.flash = 'record inserted'
return dict(form=form)
and the following “default/index.html” view:
{{extend 'layout.html'}}
{{=form}}
<script>
jQuery(document).ready(function(){
if(jQuery('#taxpayer_married').prop('checked'))
jQuery('#taxpayer_spouse_name__row').show();
else jQuery('#taxpayer_spouse_name__row').hide();
jQuery('#taxpayer_married').change(function(){
if(jQuery('#taxpayer_married').prop('checked'))
jQuery('#taxpayer_spouse_name__row').show();
else jQuery('#taxpayer_spouse_name__row').hide();});
});
</script>
The script in the view has the effect of hiding the row containing the spouse’s name:
When the taxpayer checks the “married” checkbox, the spouse’s name field reappears:
Here “taxpayer_married” is the checkbox associated to the “boolean” field “married” of table “taxpayer”. “taxpayer_spouse_name__row” is the row containing the input field for “spouse_name” of table “taxpayer”.
Confirmation on delete
Another useful application is requiring confirmation when checking a “delete” checkbox such as the delete checkbox that appears in edit forms.
Consider the above example and add the following controller action:
def edit():
row = db.taxpayer[request.args(0)]
form = SQLFORM(db.taxpayer, row, deletable=True)
if form.process().accepted:
response.flash = 'record updated'
return dict(form=form)
and the corresponding view “default/edit.html”
{{extend 'layout.html'}}
{{=form}}
The deletable=True
argument in the SQLFORM constructor instructs web2py to display a “delete” checkbox in the edit form. It is False
by default.
web2py’s “web2py.js” includes the following code:
jQuery(document).ready(function(){
jQuery('input.delete').prop('onclick',
'if(this.checked) if(!confirm(
"{{=T('Sure you want to delete this object?')}}"))
this.checked=False;');
});
By convention this checkbox has a class equal to “delete”. The jQuery code above connects the onclick event of this checkbox with a confirmation dialog (standard in JavaScript) and unchecks the checkbox if the taxpayer does not confirm: