Is there an easy way to refer to the element’s value that is calling the .val() or .text() without double selecting or caching?
For example:
//NO
$(this).val($(this).val() + 'something');
//NO
var $this;
$this.val($this.val() + 'something');
Instead, use something such as the $& token in .replace()
'some string'.replace('str', '$&123');
//results - 'some str123ing'
Thanks.
You can pass a function to
.val(), like this:This works for any number of elements, in the function
iis the index andcurrentValis what$(this).val()would get you. Just return in that function what you want the new value to be.