JQuery not working on first click, but working after it

Ciprian C

This is my code:

function getBlock2(EUID, MID, I, ESTS)
{   
      $('.new-exam').on('click', function() {
        $('.new-exam').removeClass('new-exam');
        $(this).addClass('new-exam-hover');
        $('.tab-item-hover').addClass('tab-item');
        $('.tab-item').removeClass('tab-item-hover');
      });
      $('.tab-item').on('click', function() {
        $('.new-exam-hover').addClass('new-exam');
        $('.new-exam').removeClass('new-exam-hover');
        $('.tab-item-hover').removeClass('tab-item-hover');
        $(this).addClass('tab-item-hover');     
      });

      $.ajax({
        type: "POST",
        url: './exam/x/exams_ajax.exe.php',
        data: { EUID : EUID, MID : MID, I : I, ESTS : ESTS },
        cache: false,
        success: function(data) {
        $('.block2').html(data);
      }

  }); return false;
}

On the first click, the ajax part works, but the remove/addClass one doesn't until I click again.

I can't seem to figure out why, any help would be appreciated. Thanks.

Raj

I would suggest add document.ready as suggested by others and then check if you are doing in below order.

  1. First include jQuery library
  2. Include js file which has getBlock2 function defined
  3. Include js file which is calling getBlock2 function

As a best practice, always refer your js files at the end of your page.

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 confirm not working on first click

From Dev

Counter only working on the first click for jQuery game

From Dev

Jquery Click not working in IBM Mobile first

From Dev

jquery first click is not working, further clicks works

From Dev

my jQuery code not working on the first click?

From Dev

Jquery Click not working in IBM Mobile first

From Dev

JQuery click event only working first time

From Dev

Binding Click event after append Jquery not working

From Dev

Jquery Click Handler Not Working After Two Times

From Dev

jquery 1.7 click event after append not working

From Dev

jQuery on button click not working after append

From Dev

jQuery - click function not working after remove() is used

From Dev

jQuery click function not working after get request

From Dev

datatables jquery click event not working after pagination

From Dev

jQuery .click Function Not Working after a Function call

From Dev

Jquery .click not working after adding html programatically

From Dev

On click not working after appending checkbox elements in jquery

From Dev

jQuery functions not working after using $(document).click

From Dev

jQuery on click not working after ajax call

From Dev

jQuery submit working after second click only

From Dev

After appending data, click event not working jquery

From Dev

ng-click not working after first click (embedded directives)

From Dev

ng-click not working after first click (embedded directives)

From Dev

JQuery timer not working properly in second time button click after finishing first attempt

From Dev

jQuery scrolltop - not working after first use

From Dev

ng-Click not working in angularjs after clicking for the first time

From Dev

button click jquery not working

From Dev

jQuery .click(function(){ not working

From Dev

jQuery click not working in iOS?

Related Related

  1. 1

    jquery confirm not working on first click

  2. 2

    Counter only working on the first click for jQuery game

  3. 3

    Jquery Click not working in IBM Mobile first

  4. 4

    jquery first click is not working, further clicks works

  5. 5

    my jQuery code not working on the first click?

  6. 6

    Jquery Click not working in IBM Mobile first

  7. 7

    JQuery click event only working first time

  8. 8

    Binding Click event after append Jquery not working

  9. 9

    Jquery Click Handler Not Working After Two Times

  10. 10

    jquery 1.7 click event after append not working

  11. 11

    jQuery on button click not working after append

  12. 12

    jQuery - click function not working after remove() is used

  13. 13

    jQuery click function not working after get request

  14. 14

    datatables jquery click event not working after pagination

  15. 15

    jQuery .click Function Not Working after a Function call

  16. 16

    Jquery .click not working after adding html programatically

  17. 17

    On click not working after appending checkbox elements in jquery

  18. 18

    jQuery functions not working after using $(document).click

  19. 19

    jQuery on click not working after ajax call

  20. 20

    jQuery submit working after second click only

  21. 21

    After appending data, click event not working jquery

  22. 22

    ng-click not working after first click (embedded directives)

  23. 23

    ng-click not working after first click (embedded directives)

  24. 24

    JQuery timer not working properly in second time button click after finishing first attempt

  25. 25

    jQuery scrolltop - not working after first use

  26. 26

    ng-Click not working in angularjs after clicking for the first time

  27. 27

    button click jquery not working

  28. 28

    jQuery .click(function(){ not working

  29. 29

    jQuery click not working in iOS?

HotTag

Archive