1

I want to disable and enable a click event of the button after clicking it. Hide and show works. Unbind also works but rebinding does not works. Once the button is disabled it cannot be clicked again. What am i doing wrong here?

<div style="display: inline-block;">
  <a href="#" id="btnSubmit" class="icon icon-repeat" aria-hidden="$test.get("test")},${test.get("group")}">
    <i class="fa fa-repeat"></i>
  </a>
</div>

My script is below. I want to re enable the click event after I successfully receive the response:

<script type="text/javascript">
    $('.icon-repeat').on('click', function () {
        console.log("The button is clicked")
        $.ajax({
            type: 'GET',
            url: '${createLink(action: 'test')}',
            data: {test: test},
            dataType: 'text',
            beforeSend: function () {
            },
            success: function (response) {
                $('.flashMessage').html(response);

            },
            fail: function (response) {
            }
        });
    });
</script>

2 Answers 2

1

Firstly, don't use bind() or unbind(). They were deprecated a while ago. Use on() and off() instead.

Your actual issue is that you're attempting to create an event handler without specifying what logic should be executed under that event.

A better way to approach this would be to define the logic in its own function which can be added/removed in a much easier manner:

function iconRepeatHandler() {
  $('.icon-repeat').off('click');

  $.ajax({
    type: 'GET',
    url: '${createLink(action: 'testAction')}',
    data: {
      test: test
    },
    dataType: 'text',
    beforeSend: function() {},
    success: function(response) {
      $('.icon-repeat').on('click', iconRepeatHandler);
    },
    fail: function(response) {}
  });
}

$('.icon-repeat').on('click', iconRepeatHandler);
Sign up to request clarification or add additional context in comments.

9 Comments

How can i call this function in button Click event?
Please let me know your resource of knowledge.I will read from there too. Amazed by your answers and depth explanation you gave...........+1
@jennysam $('.icon-repeat').on('click', iconRepeatHandler); this line (repeated on load and in the AJAX callback) is what attaches the click handler.
can you edit your answer according to my implementation/
@AlivetoDie thank you, that's very kind of you to say.
|
0

Apart from @Rory's answer, there is another approach which you can try. Toggle the classes and based on that bind the event handler:

$('.icon-repeat.active').on('click', function () {
    console.log("The button is clicked")
    var elem = $(this);
    elem.removeClass("active");
    // document.getElementById("btnSubmit").hidden = true
    $.ajax({
        type: 'GET',
        url: '${createLink(action: 'testAction')}',
        data: {test: test},
        dataType: 'text',
        beforeSend: function () {
        },
        success: function (response) {
                elem.addClass("active");
            // document.getElementById("btnSubmit").hidden = false
        },
        fail: function (response) {
        }
    });
});

Bind the event only to those elements which has active and icon-repeat class. In the handler, remove the active class and in success add that again.

6 Comments

@jennysam yes, that's a classname
Click function is executed only if there is $('.icon-repeat').on('click', function () not $('.icon-repeat.active').on('click', function ()
@jennysam If you can modify your code to add active class, then it will work
how can i do that?
@jennysam add active class to the element in the html
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.