jQuery addClass example

Change class name on click in jQuery

HTML

<input class="test" type="text" />

CSS

.test {
  font-size: 1.5rem;
  padding: 0.5rem;
}

JavaScript

var city = 'г. Оленегорск, ';
var input = $('.test');

input.focus(function(){
		if(input.val() === '') {
    	input.val('').val(city);
    }
});

input.blur(function(){
    if (input.val() === city){
    	input.val('');
    }
});

input.on('input change', function(){
	console.log(input.val());
  var oldValue = input.val();
  var newValue = city;
  if (oldValue.length > city.length) {
    for (var i = city.length; i <= oldValue.length; i++) {
    	if (typeof oldValue[i] !== 'undefined') {
      	newValue = newValue + oldValue[i];
      }
    }
  }
  input.val(newValue);
});