Mangala Edirisinghe Mangala Edirisinghe - 1 year ago 68
jQuery Question

How to append text to text box value using jQuery?

I need append - to the present text box value without replacing present text. I tried with this code.


but it failed.

Answer Source

Though you got several(but not all) correct answers, I want to show another way to do it:

$('#date').val(function(index, value) {
    return value + '-';

.val( function(index, value) )

function(index, value)A function returning the value to set. this is the current element. Receives the index position of the element in the set and the old value as arguments.


If you don't want to use function for doing it, use this:

var $date = $('#date');
$date.val($date.val() + '-');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download