//gets the value of the first matched form field, or sets the value of all the matched form fields $.fn.formVal = function(newVal) { var self = this.get(0); if(newVal == null) { if(this.size() < 1) return null; else if(self.type == 'text') return self.value; else if(self.type == 'checkbox' || self.type == 'radio') return this.filter('input:checked').val(); else if(self.tagName == 'OPTION') return this.parent().formVal(); else if(self.tagName == 'SELECT') return this.find('option:selected').val(); else return null; } if(self.type == 'text') return this.val(newVal); else if(self.type == 'checkbox' || self.type == 'radio') { this.find(':checked').removeAttr('checked'); this.filter(function() { return (this.value == newVal); }).attr('checked', 'checked'); return this; } else if(self.tagName == 'OPTION') return this.parent().formVal(newVal); else if(self.tagName == 'SELECT') { this.find('option:selected').removeAttr('selected'); var options = this.find('option'); var match = options.find('@value=' + newVal); if(match.size() < 1) match = options.contains(newVal); match.attr('selected', 'selected'); return this; } else return this; };