How to "append" a value with jQuery instead of replace it completely? -
the way understand val works when
$(".foo").val("x")
you literally replace value input. in jquery 2.x support "append" function instead? found unit/integration scenario let me reproduce real ... except val replaces input's value instead of "just adding single char" end user would.
access callback function of .val( cb )
, return current value concatenated string of choice:
$(".foo").val(function(i, currval) { return currval + "whatever"; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input class="foo" tyle="text" value="a-"> <!-- a-whatever --> <input class="foo" tyle="text" value="b-"> <!-- b-whatever -->
if want jquery method .valappend()
can create one:
$.fn.valappend = function( str ) { return this.val( this[0].value + str ); }; // use like: $(".foo").valappend( "whatever" );
Comments
Post a Comment