3

I have html code like this:

<input type="text" name="search1" class="search" id="search_term" onkeyup="check_search(e,this.value,'searchkey');">

And i want to unbind this key event on the document ready.

JBNavadiya
  • 109
  • 1
  • 3
  • 11

2 Answers2

5

document.getElementById('search_term').onkeyup = null;

Inline event handler attributes are assigned to the oneventname property of the element, so just set it to null to detach the event listener.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
4

If you want a JavaScript only solution

You can just set the event listener to null.

document.getElementById("search_term").onkeyup = null;

Refer to this answer for more details: https://stackoverflow.com/a/803984/3747743

If you use jQuery

You have to use the unbind method.

An example:

var handler = function() {
  alert( "The quick brown fox jumps over the lazy dog." );
};
$( "#foo" ).bind( "click", handler );
$( "#foo" ).unbind( "click", handler );

For more informations: http://api.jquery.com/unbind/

Community
  • 1
  • 1
Nicolas Henrard
  • 843
  • 8
  • 19
  • I don't see a jQuery tag on the question, nor any mention of jQuery in the question body. – Niet the Dark Absol Jul 10 '14 at 14:14
  • Basically stealing my answer now :p I'm kidding ^_^ But it still baffles me why people would use jQuery for something that can be done with *less* code in Vanilla JS... – Niet the Dark Absol Jul 10 '14 at 14:22
  • 1
    I don't steal your answer, I just provide two solutions. I think this page can be visited by a lot of people because it's a common question, so I try to be as complete as possible. – Nicolas Henrard Jul 10 '14 at 14:24
  • That's why I said I'm kidding. It's just that you ended up adding, character-for-character bar the use of double-quotes, the exact same code as mine ;) – Niet the Dark Absol Jul 10 '14 at 14:25
  • It's working for onkeyup. but same is not working for `` How to unbind this onclick? I had tried it with unbind but It's not working. – JBNavadiya Jul 11 '14 at 06:03