0

Js Code:

I added a .on("click",...) to a certain Element.

$("#sq" + nextPossibleIndices[i]).on("click", function () {...});

Then i remove it using .unbind('click').

$("#sq" + nextPossibleIndices[i]).unbind('click');

So adding it and then deleting it works fine, but after deleting it once it cannot be added again to that specific html element.

Marc B.
  • 84
  • 1
  • 8
  • What you describe should work in theory, so you should create a [mcve] so we can take a look. You could also check a variable inside the click handler instead. – Chris G Feb 25 '19 at 21:14
  • 1
    Possible duplicate of [How to remove "onclick" with JQuery?](https://stackoverflow.com/questions/1687790/how-to-remove-onclick-with-jquery) – Obsidian Age Feb 25 '19 at 21:14
  • instead of adding and removing, can you just add the logic inside of the click to cancel it? – epascarello Feb 25 '19 at 21:17
  • You should be able to add it again. Can you show how you are adding it back? You also should use off() and not unbind() – epascarello Feb 25 '19 at 21:18
  • 2
    when using .on(), you should use .off() instead of unbind. Also, if you want to remove and then add the function again, you should save the function somewhere or make it non-anonymous – ControlAltDel Feb 25 '19 at 21:19
  • I cant figure out to make it work with off(). Useing: $(".sq").on("click", function (...)); and later: $(".sq").off(); does not delete the buttons. The class ".sq" contains 64 divs, perhaps it isn't the right way to use on() and off() like this. Also saveing the functions seems to komplex for this case. I basically just want to remove every .on("click",f(...)) and be able to add them later on. – Marc B. Feb 25 '19 at 22:18
  • Maybe you should show some code. A simple example of how you add and remove the events and re-add them would go a long way. Maybe the logic is wrong somewhere else which is causing your issue. – epascarello Feb 26 '19 at 15:33
  • Actually it was the logic somewhere else and I fixed it with .off(). This was my first question on stackoverflow and i never expected that several people will try to help me with my problem, really awsome. Thanks! – Marc B. Feb 27 '19 at 09:02

3 Answers3

0

You always can keep the state yourself, something like this (I replaced unbind with off, because unbind is deprecated):

// Maintain the click handlers in an array.
const clickHandlers = [];

// Add the relevant click handlers:
clickHandlers.push(function(ev) {
  console.log('Do something on click');
});

// Later, set them:
$("#sq" + nextPossibleIndices[handlerIndex]).on(clickHandlers[handlerIndex]);

// Remove them:
$("#sq" + nextPossibleIndices[handlerIndex]).off(clickHandlers[handlerIndex]);

// Then, add them again:
$("#sq" + nextPossibleIndices[handlerIndex]).on(clickHandlers[handlerIndex]);
amedina
  • 2,370
  • 1
  • 12
  • 31
  • I guess keeping track of the states makes sense. Since my case was rather easy there was no need for it - .off() fixed it for me. Thanks anyway! – Marc B. Feb 27 '19 at 09:16
0

I think you might be looking for the .off() method, which replaced unbind after it was deprecated a while back. In your code it would look like this:

$("#sq" + nextPossibleIndices[i]).off('click');

If you're having difficult with it not working, try double-checking that you're selecting the correct element. In a pinch you can confirm that at least the method (off) works by trying it in the Chrome/Firefox consoles and removing click bindings from everything ala: $('*').off('click'); You wouldn't want to use that in actual site code, of course.

J.T. Blum
  • 314
  • 1
  • 9
-5

Try this mechanism

To remove onclick event

$("#sq" + nextPossibleIndices[i]).removeAttr("onclick");

It will remove the onclick attribute

M.suleman Khan
  • 501
  • 4
  • 17
  • The example code posted indicates that the event handler isn't stored as an attribute, so this wouldn't do anything. – J.T. Blum Feb 25 '19 at 21:42