Jörn Zaefferer wrote: > Currently val() is only a shortcut, but doesn't encapsulate anything useful. > It would be nice to have it handling some more stuff. > > I wouldn't like to have it in an external plugin, that makes it difficult to > access or find it when you actually need that functionality. > > Some thoughts about the implementation: > - single select: Nothing to worry about, right? > - multiple select: Get it's option:selected children and put their values > into an array, empty array when nothing is selected > - radio: Find the input:radio siblings with the same name and get the value > from the one that is checked, ??? when nothing is selected > - checkbox: combination of radio and select: Find input:checkbox siblings > that are checked and put their values into an array, empty array when nothing > is checked > - everything else: stick to the simple value
As a start, heres what I use: (It doesn't cover radio buttons as described above - as I never needed it) It returns an array of values if the jquery has several elements, it maybe more useful to return an object (id/name => value) instead. $.fn.getValue = function() { var o = []; this.each(function() { switch (this.type) { case 'checkbox': case 'radio': v = this.checked; break; case 'select-one': v = this.selectedIndex >= 0 ? (this.options[this.selectedIndex].value || this.options[this.selectedIndex].text) : null; break; case 'select-multiple': v = []; for (var i = 0; i < this.options.length; i++) { if (this.options[i].selected) v.push(this.options[i].value || this.options[i].text); } break; case 'button': case 'reset': case 'submit': break; default: v = this.value; } o.push(v); }); return o.length > 1 ? o : o[0]; }; _______________________________________________ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/