0

I have a div that have an onclick event, what I want is activate it or deactivated by clicking on other divs. I don't want to remove it, because I'll use thos onclick later

 <div id=goTo onclick="location.href='post.php?id=21';"> go to this page/div>

those are the buttons to click

<div id=deactivate>[click to deactivate]</div>
     <div id=reactivate>[click to reactivate]</div>

I want to deactivate/reactivate #goTo. I've tried .removeAttr() but I don't want to removed because I don't know how to restore it later

$("#deactivate").click(function(){
             $("#goTo").removeAttr("onclick");
   });
Cœur
  • 37,241
  • 25
  • 195
  • 267
joe
  • 69
  • 1
  • 2
  • 11
  • Possible duplicate of [How to remove "onclick" with JQuery?](https://stackoverflow.com/questions/1687790/how-to-remove-onclick-with-jquery) – Farhan Haque Mar 07 '18 at 18:06

1 Answers1

0
  • You can keep a flag, which you can toggle on click of reactivate and deactivate and use that to change the url based on this value.

  • Instead on inline onclick use .on("click", callback)

See the code below:

var flag = true;

$("#goTo").on("click", function(){
   if(!flag)
    return;
   location.href='post.php?id=21';
});


$("#deactivate").on("click", function(){
  flag = false;
})

$("#reactivate").on("click", function(){
  flag = false;
})
void
  • 36,090
  • 8
  • 62
  • 107
  • Ok thank you, the problem is that I already have set different configurations with this `onclick event`, so is better to keep it within the `div`as an attribute. I thought something like that but I would need to change a lot if I add this flag var. I think it is possible to find a solution as `onclick` as an attribute – joe Mar 07 '18 at 17:39