slier
slier

Reputation: 6740

Preventing hardcoded javascript event

<select onchange="window.location='/staff/add_combo.php?page='+this[this.selectedIndex].value+'&amp;ipp=2';return false" class="paginate">

Is there any way to prevent onchange event on this element?

Upvotes: 0

Views: 166

Answers (3)

Shakakai
Shakakai

Reputation: 3554

Do you just want to remove the handler permanently or temporarily?

//grabs first select on page
var mySelect = document.getElementsByTagName('select')[0];

// permanently
mySelect.onchange = function(){};//noop

//temporarily
var toggle = true;
var orig  = mySelect.onchange;
mySelect.onchange = function(){
  if(toggle) toggle = false;
  else orig.call(mySelect);
};

The permanent version just blows out the existing handler.

The temporary version saves the original onchange handler and calls it depending on some other state in the application (in this case the "toggle" variable).

Upvotes: 2

the JinX
the JinX

Reputation: 1980

If by preventing you mean removing the event listener, than yes. Just use the removeEventListener() method.

document.getElementById("mySelect").removeEventListener('onchange', function(), false);

The answer By Any E should work as well, it's the traditional way of handling it.

Upvotes: -1

Andy E
Andy E

Reputation: 344517

You can't prevent the event from firing on a single occasion, but you can unset it by assigning null to the element's onchange property:

document.getElementById("mySelect").onchange = null;

Upvotes: 1

Related Questions