Here’s another simple jQuery snippet I like to add to my projects. This will allow you to present a javascript confirm dialog when a user clicks on a link or a submit button with a confirm attribute it. The value of the confirm attribute will be the text displayed in the confirm dialog box. I often use this for delete button so the user has a chance to back out.
$(function() {
$("a[confirm], input[type=submit][confirm]").click(function() {
conf = confirm($(this).attr("confirm"));
if(!conf)
return false;
});
}
This simple little script often comes in quite handy when I’m working on a web app. It allow you to add an href attribute to tr, td, or input[type=button] tags to make them clickable links
$(function() {
$("input[type=button][href], tr[href], td[href]").click(function() {
location.href = $(this).attr("href");
});
});
I’ve encountered situations using Django’s ModelForm where I’ve needed the form to validate differently then the default behavior in various instances.
Here’s an example of how I changed all the fields from the default of not being required to all being required without much effort:
class AddressRequiredForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(AddressForm, self).__init__(*args, **kwargs)
for name, field in self.fields.iteritems():
if name not in ['address2',]:
field.required = True
class Meta:
model = Address
This is pretty simple, but it saved me a lot of time by not having to re-type each field name with a new set of properties.