简体   繁体   中英

Show span on input focus

I have two text fields that bind with <span> to count total length of a characters.

I have applied ng-if condition.

But my scenario is that it should only show characters left text when the respective textarea is focused .

在此处输入图片说明

<textarea ng-model="goal.goal_name"
 required ng-focus="openFullcreateGoalController()" maxlength="90" placeholder="Write your title here"></textarea>
 <span ng-if="goal.goal_name && !goal.goal_description">{{90 - goal.goal_name.length}}characters left</span>

  <textarea msd-elastic ng-model="goal.goal_description" required placeholder="Add description" maxlength="140"></textarea>
   <span ng-if="goal.goal_description">{{90 - goal.goal_description.length}}characters left</span>

My code only shows it if another one is empty.

You can use the css :focus rule

 var app = angular.module('my-app', [], function() {}) app.controller('AppController', function($scope) { $scope.goal = { goal_name: 'goal_name', goal_description: 'goal_description' }; }) 
 textarea.charleft + span { display: none; } textarea.charleft:focus + span { display: inline; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="my-app"> <div ng-controller="AppController"> <textarea ng-model="goal.goal_name" class="charleft" required maxlength="90" placeholder="Write your title here"></textarea> <span ng-if="goal.goal_name">{{90 - goal.goal_name.length}}characters left</span> <textarea msd-elastic class="charleft" ng-model="goal.goal_description" required placeholder="Add description" maxlength="140"></textarea> <span ng-if="goal.goal_description">{{90 - goal.goal_description.length}}characters left</span> </div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM