Roman Nrokov Roman Nrokov - 1 month ago 9
jQuery Question

Add value to input by click

I have an input field, by click value become * when I lose my focus field become clean. How to add additional * to input by every single click?

$('body').append("<input type='text' />");

$('input').click(function() {
$(this).val('*');
});

$('input').mouseleave(function() {
$(this).val('');
});

Answer

You are replacing the value of the input field. You want to append to it. Change $(this).val('*'); to $(this).val($(this).val() + '*'));