Reputation: 1111
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.
Upvotes: 7
Views: 32387
Reputation: 594
I was just doing this. I used .append(myValue); here I'm looping through an Ajax result set:
$.each(result, function (i, obj) {
$.each(obj, function (i, item) {
$('#txtValueList').append(item);
})
})
Upvotes: 0
Reputation: 150273
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() + '-');
Upvotes: 16
Reputation: 60797
The most elegant way I've found includes not using jQuery so much.
if ( len.length === 4 ) {
var date = $( '#date' )[0] // faster to write than "document.getElementById( 'date' )"
date.value += '-'
}
Fiddle: http://jsfiddle.net/Ralt/T9mUT/3/
Upvotes: 2
Reputation: 14737
You've got to retrieve the current value, and append to that.
var $date = $('#date');
$date.val($date.val() + '-');
Upvotes: 5