-3

I wan to display a message into a div rather than alert it, then hide it on click/blur The alert is popping up due to an error of typing. I need to display a message into a div and not an alert.

Something like:

$('form.form-calculator input').on('change click', function() {
        if ($(this).val() == 0) {
            var div = $("#AlertMessage");
            div = $("<div id='AlertMessage' onclick='$(this).hide();'></div>");
            $("body").prepend(div);
            this.value=0;
        }
        calc_total();
    });

With the Alert

 $(function () {
       $('.form input').on('change click focus', function () {
          if ($(this).val() == 0) {
            alert('HELLO');
            this.value = 0;
          }
          calc_total();
       });
    });
DD77
  • 1,367
  • 2
  • 13
  • 25

2 Answers2

0

Assuming both pieces of your code work (since you didn't ask to troubleshoot those), you can just combine those into one like this:

 $(function () {
       $('.form input').on('change click focus', function () {
          if ($(this).val() == 0) {
            div = $("<div id='AlertMessage' onclick='$(this).hide();'>HELLO</div>");
            $("body").prepend(div);
            this.value = 0;
          }
          calc_total();
       });
    });
Sergey Snegirev
  • 1,016
  • 1
  • 7
  • 23
0
$(function () {
    $('.form input').on('focusout', function () {
        if ($(this).val() == 0) {
            showError('Hello');
        } else {
            $("#AlertMessage").remove();
        }
        calc_total();
    });

    function showError(text) {
        $("#AlertMessage").remove();
        $('<div/>', {
            id: 'AlertMessage',
            text: "Alert! It can't be 0"
        }).prependTo('body');
        // Close on click
        $('body').delegate('#AlertMessage', 'click', function () {
            $(this).hide();
        });
    }
});
DD77
  • 1,367
  • 2
  • 13
  • 25
Axel A. García
  • 683
  • 9
  • 21
  • it should be integrated to my code. $('.form input').on('change click focus', function () { if ($(this).val() == 0) { alert('HELLO'); this.value = 0; } calc_total(); }); – DD77 Apr 11 '13 at 15:08