The jQuery function, $(...)
doesn't return the element directly. It returns a jQuery object that represents a list of one or more matching elements*.
If the selector expression matches a single element, you can use the get()
function to retrieve it and work with it directly:
$('#sometextboxaspcontrol').get(0).value +=txt;
If the selector expression matches multiple elements, and you want to append to each element's value independently, you need to iterate over the elements in some way. Again, you could use get()
(without an index) to get an array of raw elements. You can, however, use the each()
function to specify a function that will execute in the context of each element in the match list.
If this is something you do a lot, you can choose to add your own function to the jQuery object itself (i.e. create a plug-in). Typically this will result in smaller, simpler, more readable scripts with less repeated code. This is what the example does. The appendVal()
function is added to the jQuery object (and it also returns the jQuery object (returned by the each()
function) to allow you to call other jQuery functions in a "chain", e.g.
$('#sometextboxaspcontrol').appendVal(txt).addClass('myClass');