0
<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?

3
  • How about simply changing it? It's not clear what you would like to achieve. Commented Feb 7, 2011 at 9:47
  • Remove the onchange attribute from the element? Commented Feb 7, 2011 at 9:50
  • i simply want to remove the onchange event..the link is being generated by one of php class, i dont feel like changing the class just for the sake to remove the onchange event Commented Feb 7, 2011 at 14:50

3 Answers 3

2

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).

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

Comments

1

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;

Comments

-1

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.

1 Comment

This isn't technically correct; in his original example, that needs to be removed as an Attribute.

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.