Skip to content Skip to sidebar Skip to footer

How To Run JQuery Code In The JavaScript Onclick Function?

Clicking on the 'close' anchor does not close the notification. Below is my code: function show_notification_on_top(message, type) { content = '

Solution 1:

Don't hardcode onclick events on <a>links, use JQuery click unobtrusive subscriber.

function show_notification_on_top(message, type) {  

    content =           
                "<a class='notify-close' href='#'>close</a>"+
                "<p> message </p>";                   

    $("#notification-box").fadeIn('slow', function() {
            $("#notification-box").delay(60000).fadeOut('slow');
        });

    $("#notification-box").html( content ); 

    $('.notify-close').click(function(){
            $('#notification-box').dequeue();
        });
}

Solution 2:

Haven't tried the code, but you want something like this...

function show_notification_on_top(message, type) {                  

    var anc = $('<a>').addClass('notify-close').html('close').click(function() {$('#notification-box').fadeOut();   });

    $("#notification-box").append( anc ).fadeIn('slow', function() {
            $("#notification-box").delay(60000).fadeOut('slow');
    });


}

Solution 3:


Solution 4:

Thanks all of you. With your help, I've written this code. Works perfectly fine.

function show_notification_on_top(message) {

    content =       "<a class='notify-close' id='notification_anchor' href='#'>close_button_label</a>"+ 
                    "<p>"+message+"</p>";


    $("#notification-box").fadeIn('slow');

    $("#notification-box").html( content );

    $('#notification_anchor').click(function() {
        $("#notification-box").fadeOut("slow");
    });

    window.setTimeout(function() {
        $("#notification-box").fadeOut('slow');
    }, 6000);
}

Solution 5:

Add this:

$(document).ready(function(){
  $(".notify-close").live("click", function(){
    $("#notification-box").fadeOut('slow');
  });
});

and forget about the onclick() event;


Post a Comment for "How To Run JQuery Code In The JavaScript Onclick Function?"