-2

I want to use click() as eventhandler and that event handler is not working you can see code below

    $('.ajax-close').click(function( event ){
        event.preventDefault();
        alert('hi');
        $( '.ajax-live-on' ).removeClass('ajax-live-on');
    });

I have used all the code to initialize the jquery no problem , all right. But this piece of code not working

Here is the jsBin link

http://jsbin.com/doxeravizo/1/edit?html,css,js,output

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
muhaimin
  • 119
  • 1
  • 12

5 Answers5

2

The $('.ajax-close') collection doesn't contain the elements taking that class after the binding.

Change

$('.ajax-close').click(function( event ){

to

$(document.body).on('click', '.ajax-close', function( event ){

You should also move that binding outside of the loop, there's no reason to do it at every iteration.

Note also that in order to have your span clickable, it must have some content.

Demonstration (I added the jQuery library to make the fiddle work)

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

I'm guessing that because you're using ajax, your .ajax-close is not created when the event listener is being created.

You're going to want to delegate your click function:

$(document).on('click', '.ajax-close', function(event) {
    event.preventDefault();
    alert('hi');
    $('.ajax-live-on').removeClass('ajax-live-on');
});

This article will help, but just for reference, this bit in particular:

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

Jamie Barker
  • 8,145
  • 3
  • 29
  • 64
1

One option is to listen on the click event using a delegate, like so:

$(document).on('click', '.ajax-close', function( event ){
    //your code
});

Another option might be to move your click listener inside the original click listener, which creates the "Close" button, while the reason the issue arises is that the click event on "ajax-close" is bound too soon (before the <span> is appended to the DOM even):

ajaxcontent.click(function(event) {
    event.preventDefault();
    $( '.ajax-live' ).addClass('ajax-live-on');
    $( this ).after('<span class="ajax-close animated bounceInRight">Close</span>');
    $('.ajaxshow').append().load(ajaxUrl);
    $('.ajaxshow').addClass('animated bounceInUp');

    // Move this section here, which was previously located below  
    $('.ajax-close').click(function( event ){
        event.preventDefault();
        alert('hi');
        $( '.ajax-live' ).removeClass('ajax-live-on');
    });

});

Make sure to include some content in your "ajax-close" span to be able to click it like the word "Close".

0

Add JQuery library to your HTML head :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
freethinker
  • 2,257
  • 4
  • 26
  • 49
0

In your given link you are adding element dynamically, so need to use event delegate for dynamically created elements event binding.

$(document).on('click', '.ajax-close', function( event ){
  //your code
});
ozil
  • 6,930
  • 9
  • 33
  • 56