0

I try to disable an onclick event and bring it back. Disable it works fine with:

 <div id="rob_icon_gui" onclick="robbery()"</div>

function bye() {
document.getElementById("rob_icon_gui").onclick = '';
}

but it doesn't come back:

 <div id="rob_icon_gui" onclick="robbery()"</div>

function hallo() {
document.getElementById("rob_icon_gui").onclick = 'robbery()';
}

2 Answers 2

2

Change document.getElementById("rob_icon_gui").onclick = 'robbery()'; to

document.getElementById("rob_icon_gui").onclick = robbery;

function robbery() {
  console.log('Executing Robbery')
}

document.getElementById("rob_icon_gui").onclick = robbery;
<button id='rob_icon_gui'>Click</button>

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

1 Comment

Other way is to use .addEventListener('click', robbery) and .removeEventListener('click')
1

Your html

<div id="rob_icon_gui clickablediv"</div>

You script

var handler = function() {
  alert( "The quick brown fox jumps over the lazy dog." );
};

You can bind your click event like this

$('.clickablediv').bind('click',handler);

and you can unbind click event as well

$('.clickablediv').unbind('click',handler);

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.