Delete placeholder value in input by clicking Angularjs

Asked

Viewed 616 times

5

Does anyone know how I can erase the value of placeholder by clicking on the field using only Angularjs?

Example: When you click, automatically add the word name.

<input type="text" ng-model="inputText" placeholder="Nome" />

2 answers

3


As follows:

<input type="text" ng-model="inputText" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Nome'" placeholder="Nome" />

  • Thanks Thiago

2

With , just remembering that the normal behavior input concealed the placeholder when there is value, but, this is a way to remove placeholder on receiving the focus and when it loses focus again the value is assigned Name in the placeholder

var app = angular.module('app', []);
app.controller('ctrl', ['$scope',
  function($scope) {
    $scope.init = function() {
      var ele = angular.element("#input1")[0]
      ele.addEventListener('focus',
          function() {
              ele.setAttribute('placeholder', '');
          });
      ele.addEventListener('blur',
          function() {
              ele.setAttribute('placeholder', 'Nome');
          });
    }
    $scope.init();
  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">
  <input type="text" id="input1" ng-init="inputText=''" ng-model="inputText" placeholder="Nome" />
</div>

  • 1

    Thank you for the detailed explanation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.