Alex Man
Alex Man

Reputation: 4886

show and hide bootsrap tooltip through javascript

I have a stuff which uses ui.bootsrap tool-tip feature, the code is working fine, but I don't know to show and hide the tooltip through script, say I am having a form which some field validation, when I submit the form, if the validation for a component say (a text field for email) fails, then it should shows up a tool-tip it should not go unless the field is properly validated,

Can anyone please tell me some solution for this

script

var app = angular.module('someApp', ['ui.bootstrap']);

app.controller('MainCtrl', function ($scope) {

    $scope.validate = function () {
        var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        var emailValid = re.test($scope.userEmail);
        if(!emailValid)
        {
            // I want to show the tool tip
        }
    };

})

html

<div ng-app="someApp" ng-controller="MainCtrl">
    <form ng-submit="validate()">
        <input type="text" ng-model='userEmail' rc-tooltip="Invalid Email...." tooltip-placement="bottom" />
        <input type="submit" />
    </form>
</div>

JSFiddle

Upvotes: 1

Views: 1841

Answers (2)

Michael Kang
Michael Kang

Reputation: 52837

Demo

Here is a simple email validation directive that uses bootstrap:

app.directive('email', function() {
  return  {
    restrict: 'A',
    require: 'ngModel',
    compile: function(element, attr) {
      element.tooltip({ placement: 'right', title:'Email is invalid', trigger:'manual'});
      function emailValid(email) {

        var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
         var valid = re.test(email);
         return valid;

      }
      return  function(scope, element,attr, ngModel) {
          ngModel.$validators.email = function(val) {
            return emailValid(val);
          }

          scope.$watch(function() {
            return ngModel.$error.email;
          }, function(val) {
            if (val)
               element.tooltip('show');
            else
               element.tooltip('hide');

          });
      }
    }
  }
});

Usage

<input type="text" ng-model="email" email  />

Upvotes: 1

Anita
Anita

Reputation: 2400

Its given hereBootstrap Tooltip

And by using data-toggale option using javascript ,you can use tooltip.

$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})

By above code you can assign tooltip then to show and hide you can use $('#element').tooltip('show')

or

$('#element').tooltip('hide')

Upvotes: 1

Related Questions