0

I am trying to disable click event and disable div once I click on the item. I am unable to achieve this functionality. Any help? What I have tried

<div class="ordrClone">
                       Image 1
                    </div>
<div class="ordrClone">
                       Image 2
                    </div>
<div class="ordrClone">
                       Image 3
                    </div>

<div>
   <input type="button" id= "btnCancel" value="Enable"> 
</div>

 $('.ordrClone').click(function () {
        $('.ordrClone').not(this).each(function() {
            $(this).off('click');
        });
    });

$('#btnCancel').click(function () {
     $('.ordrClone').each(function() {
            $(this).on('click');
        });
});

5 Answers 5

2

Use $(this) instead of this:

$('.ordrClone').not($(this)).each(function() {

And also create a callback function and use that callback on the on method:

$(this).on('click');//it's wrong way to do.

$(this).on('click',callback);

see on method for more detail

But if you want to use the function only for once you can use the one method instead of on method.

Sign up to request clarification or add additional context in comments.

Comments

1

One easy solution is

$(document).on('click', '.ordrClone:not(.disabled)', function () {
    console.log('clicked')
    $('.ordrClone').not(this).addClass('disabled');
});

$('#btnCancel').click(function () {
    $('.ordrClone').removeClass('disabled');
});

Demo: Fiddle

Comments

1

Use one() instead of on() like,

$('.ordrClone').one('click',function () {
      // your code worked once
});

Live Demo

Comments

1

You can use One, if you want to click once only

$(document).one('.ordrClone','click',function () {
        $('.ordrClone').not(this).each(function() {
            $(this).off('click');
        });
    });

Comments

1

$("#btnCancel").click(function() { $(this).unbind('click'); }

This should unbind the 'click' functionality of the item you just clicked. You can disable an element many different ways. A really popular way is .hide()`ing the code and using it when needed, but the above code should work fine to disable a specific event from a form element.

Comments

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.