Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

if(len.length==4){
     $("-").appendTo("#date").val();
}

but it failed.

like image 831
Mangala Edirisinghe Avatar asked Apr 24 '12 06:04

Mangala Edirisinghe


People also ask

How to append text value in jQuery?

jQuery append() MethodThe append() method inserts specified content at the end of the selected elements. Tip: To insert content at the beginning of the selected elements, use the prepend() method.

How to append text in input field?

To append an element with a text message when the input field is changed, change() and appendTo() methods are used. The change() method is used to detect the change in the value of input fields. This method works only on the “<input>, <textarea> and <select>” elements.

How to append input value in javascript?

JS : var button = document. getElementById('add-item'); var result = document.


2 Answers

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.

source

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

var $date = $('#date');
$date.val($date.val() + '-');
like image 165
gdoron is supporting Monica Avatar answered Oct 03 '22 22:10

gdoron is supporting Monica


You've got to retrieve the current value, and append to that.

var $date = $('#date');
$date.val($date.val() + '-');
like image 23
Richard Neil Ilagan Avatar answered Oct 04 '22 00:10

Richard Neil Ilagan