Dave Methvin wrote:
 >  The API says that $.val() will:
 >     Get the current value of the first matched element.
 
Yes, it is equivalent to $().attr("value") so it gets the value attribute of
the first element.
 
 >  The API says that $.val( string ) will:
 >     Set the value of every matched element.

Yes, it will set a "value" attribute for each element, regardless of whether
the doctype indicates the element has a "value" attribute. Generally you'd
only want to use .val() in cases where the element responded to having a
"value" attribute though.
  
I know that it does what it says it does. That's not the point. The point is that it only works on the input element. I'm sorry, but I think that should be spelled out. Obviously a number of people thought it would retrieve the value of the selected option(s) in a select (or multiple select) box and that it would retrieve all text between the opening and closing textarea tags. What would be wrong with adding those? There's probably a perfectly good reason, I just don't know what it is. At the very least, it would be nice to document the seemingly common misconception that this method does not handle those cases and point the reader to the proper method.
The only indications in the API that this only works on the
input element is that the examples following both methods
use the input element. There's no other indication, so I
am surprised to find that it only works on this element.
    

I think the word "works" means "makes sense" here. What should
$("div").val() return? Right now it would be undefined since usually a div
has no value attribute. I could see an interpretation where it might return
the text nodes inside the div, but $().text() already does that.

  
I don't know what I would have expected $("div").val() to return, but I, personally, probably wouldn't have thought to use it that way. I would only have thought to use it on form elements: inputs, textareas, and selects. The API also does not discuss here what this method returns if the input is a check box or radio button. Does it return that it is checked (like f.mycheckbox.checked)?

Chris
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to