Search results

Create and show Tooltip on Multiple Targets in JavaScript (ES5) Tooltip control

08 May 2023 / 2 minutes to read

Tooltip can be created and shown on multiple targets within a container by defining the specific target elements to the target property. So, the tooltip is initialized only on matched targets within a container.

In this case, the tooltip content is assigned from the title attribute of the target element.

Copied to clipboard
import { Tooltip } from '@syncfusion/ej2-popups';

let tooltip: Tooltip = new Tooltip({
    position: 'RightCenter'
});
tooltip.appendTo('#uname); // Here we have appended the target to the element.
Source
Preview
index.js
index.html
index.css
Copied to clipboard
var button1 = new ej.buttons.Button();
button1.appendTo('#sample');
var button2 = new ej.buttons.Button();
button2.appendTo('#clear');
var tooltip1 = new ej.popups.Tooltip({
    position: 'RightCenter'
});
tooltip1.appendTo('#uname');
var tooltip2 = new ej.popups.Tooltip({
    position: 'RightCenter'
});
tooltip2.appendTo('#mail');
var tooltip3 = new ej.popups.Tooltip({
    position: 'RightCenter'
});
tooltip3.appendTo('#pwd');
var tooltip4 = new ej.popups.Tooltip({
    position: 'RightCenter'
});
tooltip4.appendTo('#cpwd');
document.getElementById('sample').addEventListener('click', function(){
  var name = document.getElementById('uname').value;
  var pwd = document.getElementById('pwd').value;
  var cpwd = document.getElementById('cpwd').value;
  if(name.length > 0 & name.length < 4){
    document.getElementById('uname').title = 'Required Minimum 4 Characters';
    document.getElementById('uname').style.backgroundColor = 'red';
    var target = document.getElementById('uname');
    tooltip1.open(target);
  } else {
   
     document.getElementById('uname').style.backgroundColor = 'white';
  }
  if(pwd !== '' && pwd.length < 8){
     document.getElementById('pwd').title = 'Required Minimum 8 Characters';
    document.getElementById('pwd').style.backgroundColor = 'red';
    var pwdtarget = document.getElementById('pwd');
    tooltip3.open(pwdtarget);
  } else {
     document.getElementById('pwd').style.backgroundColor = 'white';
  }
  if(name.length >= 4 && pwd !== '' && pwd.length >= 8  &&  pwd == cpwd ){ 
     alert('Form Submitted');
  } else {
     alert('Details are not Valid');
  }
})
document.getElementById('clear').addEventListener('click', function(){
  document.getElementById('uname').style.backgroundColor = 'white';
  document.getElementById('pwd').style.backgroundColor = 'white';
  var target = document.getElementById('uname');
  tooltip1.close(target);
  document.getElementById('uname').title = 'Please enter your name';
  var pwdtarget = document.getElementById('pwd');
  tooltip3.close(pwdtarget);
});
Copied to clipboard
<!DOCTYPE html><html lang="en"><head>
            
    <title>EJ2 Tooltip</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript UI Controls">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-base/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet">
    <link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
      <form id="details" role="form">
        <div id="user">
          <div class="info">User Name:</div>
          <div class="inputs"><input type="text" id="uname" class="e-info e-input" name="firstname" title="Please enter your name"></div>
        </div>
        <br>
        <div>
          <div class="info">Email Address:</div>
          <div class="inputs"><input type="text" id="mail" class="e-info e-input" name="email" title="Enter a valid email address"></div>
        </div>
        <br>
        <div>
          <div class="info">Password:</div>
          <div class="inputs"><input id="pwd" type="password" class="e-info e-input" name="password" title="Be at least 8 characters length"></div>
        </div>
        <br>
        <div>
          <div class="info">Confirm Password:</div>
          <div class="inputs"><input id="cpwd" type="password" class="e-info e-input" name="Cpwd" title="Re-enter your password"></div>
        </div>
        <br>
        <div class="btn">
          <input id="sample" type="button" value="Submit">
          <input id="clear" type="reset" value="Reset">
        </div>
      </form>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
    ele.style.visibility = "visible";
 }   
        </script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Copied to clipboard
#container {
    visibility: hidden;
    border: 1px solid #ddd;
  }

  #loader {
    color: #008cff;
    height: 40px;
    left: 45%;
    position: absolute;
    top: 45%;
    width: 30%;
  }

#details .info {
    font-weight: bold;
    width: 165px;
    display: inline-block;
    margin-left: 10px;
}
#details .inputs {
    display: inline-block;
}
#details .btn{
  margin-top: 10px;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
  display: inline-block;
}
#details #sample{
  margin-left:10px;
}
#details #clear{
  margin-left:10px;
}
#details {
  padding-top: 30px;
  padding-bottom: 30px;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
  display: inline-block;
}