How to Create Alert Box in Bootstrap using jQuery

0 9,891

This article we are ging to discuss about How to Create Alert Box in Bootstrap using jQuery Alert boxes area unit used very often to face out the knowledge that needs immediate attention of the top users like success, warning or error confirmation messege. with Bootstrap you will be able to simply produce elegant alert box for varied functions. you can add an optional close button to dismiss alert boxes. You can see that the close function is apply to all the alert boxes for example close any alert message, rest of the alert box also gets close. Alert Box are a greate way to show quick information to users and to also alert them of errors, warning, success messages for information. when you bring jQuery into the mix.

How to Create Alert Box in Bootstrap using jQuery

Alert messages are mostly used to show information such as success, warning or cinfirmation message to the users. Fllow these step to create alert box in Bootstrap using Jquery.

Getting Bootsrap

Bootstrap can be Download to your needs from their Getting Started page but i would prefer using the CDN option, because it is faster also it is advised to go through and get yourself accustomed with some bootstrap terms, including common classes. This page also some Examples of how to use Bootstrap classes.

Adding Bootstrap Library

after download bootstrap we need to bootstrap library file in your page and in this step we going to add library file.

<link type="text/css" href="css/bootstrap.min.css" rel="stylesheet">

Import alert.js Plugin

alert.js is a simple jQuery plugin that creates customizable alert boxes to replace the default defualt loading, alert, confirm and prompt dialog boxes.

How To use it

Just load jQuery library together with the jQuery alerts plugin file.

<script src="js/jquery.js"></script>
<script src="js/alert.js" type="text/javascript"></script>

Creating Alert Box Controller in HTML using Bootstrap

in this step we are going to create a alert box controler to choose whats type of alert box seen.

<form class="form-horizontal" role="form" style="width: 728px;margin: auto;">
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <h1>Alert Box Demos</h1>
        </div>
    </div>
   
    <div class="form-group">
        <label for="isOnly" class="col-sm-2 control-label">Multiple instances</label>

        <div class="col-sm-10">
            <div class="checkbox">
                <input id="isOnly" type="checkbox" checked="checked" style="margin-left: 0"/>
            </div>
        </div>
    </div>
    <div class="form-group">
        <label for="autoClose" class="col-sm-2 control-label">Auto close</label>

        <div class="col-sm-10">
            <div class="checkbox">
                <input id="autoClose" type="checkbox" checked="checked" style="margin-left: 0"/>
            </div>
        </div>
    </div>
    <div class="form-group">
        <label for="withTime" class="col-sm-2 control-label">countdown timer</label>

        <div class="col-sm-10">
            <div class="checkbox">
                <input id="withTime" type="checkbox" style="margin-left: 0"/>
            </div>
        </div>
    </div>
    <div class="form-group">
        <label for="withTime" class="col-sm-2 control-label">Time to auto close</label>

        <div class="col-sm-10">
            <input id="time" class="form-control" value="3" style="width: 50px;display: inline-block;"> seconds
      </div>
    </div>
    <div class="form-group">
        <label for="withTime" class="col-sm-2 control-label">Notification types</label>

        <div class="col-sm-10">
            <select id="type" class="form-control" style="width: 100px">
                <option value="success">success</option>
                <option value="info">info</option>
                <option value="warning">warning</option>
                <option value="danger">danger</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <div class="btn-group">
                <button type="button" class="btn btn-default" data-position="top-left">Top left</button>
                <button type="button" class="btn btn-default" data-position="top-right">Top right</button>
                <button type="button" class="btn btn-default" data-position="top-left">Center</button>
                <button type="button" class="btn btn-default" data-position="bottom-left">Bottom left</button>
                <button type="button" class="btn btn-default" data-position="bottom-right">Bottom right</button>
            </div>
        </div>
    </div>
</form>

Creating Script in jQuery

This step we are going to create script in jquery for alert box.

<script type="text/javascript">
    $(function () {
        $('button').on('click', function () {
            $.alert('jQuery alert.js Plugin!', {
                title: 'jQuery Script',
                closeTime: $('#time').val() * 1000,
                autoClose: $('#autoClose').is(':checked'),
                position: [$(this).data('position')],
                withTime: $('#withTime').is(':checked'),
                type: $('#type').val(),
                isOnly: !$('#isOnly').is(':checked')
            });
        });
    })
</script>

If you facing any type of problem with this source code then you can Download the Complete source code in zip Formate by clicking the below button Download Now otherwise you can send Comment.

Download Source Code

Leave A Reply

Your email address will not be published.