addClass() function doesn't work after ajax call

sin

I'm making a simple AJAX call that it is getting trigger after someone clicks an anchor. After the AJAX is successful I would like to add a class at the anchor that triggered the AJAX.

Although the script is working fine, and the success function returns all the correct data, when I try to addClass() it doesn't work. I used a hide() method to see if jQuery runs correctly and it doesn't work either. The console doesn't print any errors.

For debugging I used an alert and it works! How can the alert work fine and both addClass() and hide() not?

<a href="#" class="refreshor" value="20">1</a>
<a href="#" class="refreshor" value="40">2</a>
jQuery(document).ready(function($) {
    $('.refreshor').on('click',function(e){
        e.preventDefault();
        var search = $('#searchin').val();
        var page = $(this).text();
        var that = this;
        $.ajax({    
            type: "POST",
            url: "components/com_tagger/scripts/ajaxSearch.php",
            data: {
                "search": search,
                "page": page
            },
            success:function(response) {
                $('.ajaxSearchResults').html(response);
                $(that).addClass('preventer'); //this doesnt work
                $(that).hide(); //this doesnt work
                var test = $(that).text(); 
                alert(test); //this works!                  
            } 
        });
    });
});
Hacketo

Inside the success callback you replace the content of .ajaxSearchResults

$('.ajaxSearchResults').html(response);

Then that is not referring to the ones that are inside this container.

You need to use delegate, that will allow you to bind events on elements that not yet in the DOM

$('.ajaxSearchResults').on('click', '.refreshor',function(e){
    e.preventDefault();
    var search = $('#searchin').val();
    var page = $(this).text();

    var thisValue = $(this).attr('value'); // save the current value of the `a` clicked
    $.ajax({    
        type: "POST",
        url: "components/com_tagger/scripts/ajaxSearch.php",
        data: {
            "search": search,
            "page": page
        },
        success:function(response) {
            $('.ajaxSearchResults').html(response);

            var that = $('.ajaxSearchResults').find('.refreshor[value="'+thisValue+'"]'); 
            // look for the `a` that have the saved value.

            that.addClass('preventer');
            that.hide(); 
            var test = that.text(); 
            alert(test);        
        } 
    });
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jquery function doesn't work after Ajax call

From Dev

addClass() function not working after AJAX call

From Dev

After an AJAX call, the next AJAX call doesn't work

From Dev

Javascript doesn't work after call a function

From Dev

Click function doesn't work after ajax call in dynamic element (Backbone)

From Dev

Click function doesn't work after ajax call in dynamic element (Backbone)

From Dev

Change inner text after ajax call back doesn't work?

From Dev

AJAX call doesn't work after submitting a form

From Dev

ajax doesn't work properly when call inside a function

From Dev

addClass doesn't work (jQuery)

From Dev

The .addClass() doesn't seem to work

From Dev

addClass doesn't work (jQuery)

From Dev

Ajax Call Worked; Doesn't Work Anymore

From Dev

Jquery ajax call doesn't work on mobile

From Dev

Javascript hover addclass not working after Ajax call

From Dev

javascript ajax doesn't work after caching

From Dev

jquery datepicker doesn't work after ajax call if its already on page

From Dev

Assembly - Why this CALL function doesn't work?

From Dev

php echo javascript function after an ajax call doesnt work?

From Dev

php echo javascript function after an ajax call doesnt work?

From Dev

Ajax call doesn't work within click() in Javascript

From Dev

Ajax call doesn't work from iPhone app and arduino

From Dev

Ajax call doesn't work within click() in Javascript

From Dev

DropZone Plugin doesn't work when I call it with ajax

From Dev

Ajax page onclick doesn't work when function defined in $(function(){ })

From Dev

Rails doesn't display flash messages after ajax call

From Dev

PHP call from JavaScript function via AJAX to store data in mySQL(AMPPS localhost) just doesn't seem to work

From Dev

jQuery function doesn't work after after loading dynamic content

From Dev

MVC RedirectToAction Doesn't Work After JQuery/ Ajax Post

Related Related

  1. 1

    Jquery function doesn't work after Ajax call

  2. 2

    addClass() function not working after AJAX call

  3. 3

    After an AJAX call, the next AJAX call doesn't work

  4. 4

    Javascript doesn't work after call a function

  5. 5

    Click function doesn't work after ajax call in dynamic element (Backbone)

  6. 6

    Click function doesn't work after ajax call in dynamic element (Backbone)

  7. 7

    Change inner text after ajax call back doesn't work?

  8. 8

    AJAX call doesn't work after submitting a form

  9. 9

    ajax doesn't work properly when call inside a function

  10. 10

    addClass doesn't work (jQuery)

  11. 11

    The .addClass() doesn't seem to work

  12. 12

    addClass doesn't work (jQuery)

  13. 13

    Ajax Call Worked; Doesn't Work Anymore

  14. 14

    Jquery ajax call doesn't work on mobile

  15. 15

    Javascript hover addclass not working after Ajax call

  16. 16

    javascript ajax doesn't work after caching

  17. 17

    jquery datepicker doesn't work after ajax call if its already on page

  18. 18

    Assembly - Why this CALL function doesn't work?

  19. 19

    php echo javascript function after an ajax call doesnt work?

  20. 20

    php echo javascript function after an ajax call doesnt work?

  21. 21

    Ajax call doesn't work within click() in Javascript

  22. 22

    Ajax call doesn't work from iPhone app and arduino

  23. 23

    Ajax call doesn't work within click() in Javascript

  24. 24

    DropZone Plugin doesn't work when I call it with ajax

  25. 25

    Ajax page onclick doesn't work when function defined in $(function(){ })

  26. 26

    Rails doesn't display flash messages after ajax call

  27. 27

    PHP call from JavaScript function via AJAX to store data in mySQL(AMPPS localhost) just doesn't seem to work

  28. 28

    jQuery function doesn't work after after loading dynamic content

  29. 29

    MVC RedirectToAction Doesn't Work After JQuery/ Ajax Post

HotTag

Archive