On 11/15/06, "Jörn Zaefferer" <[EMAIL PROTECTED]> wrote: > > > I often times just need to know the value of one form element and > > > sometimes that could be a group of radio buttons, a select, a multiple > > > select or just a regular input. The current $.val() method is limited > > > only to inputs. I was curious if anyone else needed this funtionality > > > and if it should be in a plugin or in the core? > > > > I would definitely like to see val() work on any form element. I > > frankly didn't know that it doesn't. It only makes sense that it > > would since there would be no point in implementing the function if it > > only replaces $('input')[0].value. I believe the true power of most of > > jQuery is the fact that it abstracts some really complex code into a > > really simple API. Having one function that will get the current value > > >from any form element (a String for most elements, or an array for > > multiple selects) would be a real win, and it should be in the core. > > > > The other argument I can see is the filesize one. Keeping the core > > jQuery filesize down is a great goal for the project. So, if you guys > > see an enhanced val() function as only required if one is working with > > forms as far as Ajax is concerned, then perhaps it should be enhanced > > as part of the ajax.js file in the core so that it can be easily > > removed if no ajax stuff is needed for a project. Otherwise, the only > > other place I see it being appropriate is as part of the Form plugin. > > > > I would personally like to have it in the core. Getting and setting > > info about DOM elements is the bread-and-butter of jQuery, so a more > > powerful val() function would be a good use of bandwidth in my mind. > > 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 > > For radios/checkboxes: Can we rely on searching for siblings? Or do we need > to search for the name inside the current form? > > I have this in my validation plugin to check the number of selected > checkboxes/radios: > > return $(element).parents('form, body').find('[EMAIL PROTECTED]' + > element.name + ']:checked').length;
I've used something similar which, as I understand, should be a little more efficient: return $(element.form).find('[EMAIL PROTECTED]' + element.name + ']:checked').length; -- Choan <http://choangalvez.nom.es/> _______________________________________________ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/