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.
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.
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.
JS : var button = document. getElementById('add-item'); var result = document.
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() + '-');
You've got to retrieve the current value, and append to that.
var $date = $('#date');
$date.val($date.val() + '-');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With