Click here to Skip to main content
15,892,298 members
Articles / Web Development / ASP.NET
Tip/Trick

Balloon Message using JavaScript & CSS in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.96/5 (14 votes)
18 Feb 2015CPOL 26.4K   660   25   3
This is a very simple way to create a balloon message... without using any plugin

Introduction

Most aspects of balloons message can be controlled through the plugin...

In this tip, I will show how to create a balloon message in a simple way using JavaScript & CSS.

Image 1

Using the Code

How to achieve balloon message without using any plugin:

Step A: Find coordinates of control where to show balloon message.

Step A.1: Write function to find coordinates of control like TextBox, dropdown, etc.

JavaScript
function getOffsetRect(elem) 
{
 var box = elem.getBoundingClientRect()
 var body = document.body
 var docElem = document.documentElement

 var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop
 var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft

 var clientTop = docElem.clientTop || body.clientTop || 0
 var clientLeft = docElem.clientLeft || body.clientLeft || 0

 var top = box.top + scrollTop - clientTop
 var left = box.left + scrollLeft - clientLeft

 return { top: Math.round(top), left: Math.round(left) }
}

Step A.2: Write a function to show balloon message & validate your control with the help of CSS & JavaScript.

The below function requires three parameters:

  1. ControlID like TextBox, DropDown, etc.
  2. SpanID -->(span tag ID) for adding a hook to a part of a balloon message
  3. Duration -->Hide Error message after specific time 
  4. Error Message
CSS
function RequiredFieldValidation(TextBoxID, SpanID,Duration,ErrorMsg) {
    var j = getOffsetRect(TextBoxID);
    
    AutoHide(SpanID, Duration);

    var Width = TextBoxID.clientWidth + 10 + j.left;
    if (TextBoxID.value == "") {
        SpanID.className = "ShowBubble";
        SpanID.style.top = j.top + "px";
        SpanID.style.left = Width + "px";
        SpanID.innerHTML = ErrorMsg;
        return false;
    }
    return true;
}

CSS-->

.ShowBubble
{
    z-index: 1;
    position: absolute;
    margin-top: -3px;
    padding: 6px 5px;
    background: #ffc477;
    -webkit-border-radius: 6px;
    -moz-border-radius: 6px;
    border-radius: 5px;
    width: Auto;
    height: Auto;
    font-style:italic;
}
.HideBubble
{
    display: none;
}
.ShowBubble:after
{
    content: '';
    position: absolute;
    border-style: solid;
    border-width: 15px 15px 15px 0;
    border-color: transparent #ffc477 transparent;
    display: block;
    width: 0;
    z-index: 1;
    left: -9px;
    top: -1px;
}

If you would like to learn more about JavaScript coordinates, refer to the below link:

History

  • 14th February, 2015: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionSpecify AutoHide() method Pin
D V L18-Feb-15 18:46
professionalD V L18-Feb-15 18:46 
QuestionAutoHide(SpanID, Duration); Pin
Kurt Muellner18-Feb-15 9:31
Kurt Muellner18-Feb-15 9:31 
AnswerRe: AutoHide(SpanID, Duration); Pin
sgoodrow19-Feb-15 14:14
sgoodrow19-Feb-15 14:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.