Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Remove onClick event with javascript

Featured Replies

HI All

 

So I have an issue with an <input> element which at some point during page load has an onClick evet givent to it by a script which is unaccessable to me. However I need to find a way to remove the event once the page has loaded using javascript.

 

 

I have tried a number of things including adding scrippt to the bottom of the page as follows:-

 

 

 

HTML


<input id="btnConfirm" >

 

javaScript


var btnC = getElementById("btnConfirm");

if(btnC.onclick == true) {

this.onclick = null;


};

 

I realise I may be approcing this from the wrong angle but I am dealing with a rather old complcated system here and just need to get this one issue resolved before I can sign it off.

 

Obviously appreciate any suggestions.

 

Thanks

Put the following just before the </body> tag

<script type="text/javascript">
window.onload = function () {
	document.getElementById("btnConfirm").onclick = null;
	}
</script>

 

On second thoughts, after reading your post properly, that might override an already existing 'window.onload' event, which isn't so good.

Instead you could do

var oldEvent = (window.onload) ? window.onload : function () {};
window.onload = function () {
   	oldEvent();
	document.getElementById("btnConfirm").onclick = null;
	}

Edited by ElanMan

Try this, not sure if it will work but it looks about right.

 

document.getElementById("btnConfirm").removeEventListener("click", function(e) { e.preventDefault(); }, false);

The moz specs say the 2nd argument is suppose to be a reference to the function which is to be be removed so you'll need to put that in instead of that function i put in.

  • Author

Put the following just before the </body> tag

<script type="text/javascript">
window.onload = function () {
	document.getElementById("btnConfirm").onclick = null;
	}
</script>

 

On second thoughts, after reading your post properly, that might override an already existing 'window.onload' event, which isn't so good.

Instead you could do

var oldEvent = (window.onload) ? window.onload : function () {};
window.onload = function () {
   	oldEvent();
	document.getElementById("btnConfirm").onclick = null;
	}

 

 

Well done Elman you are the winner the first one worked perfectly but as you say there is a window.load event already so probably better to use your second suggestion however this one didnt work but I just modified it by adding a time out so it does its stuff well after everything has loaded.

 

I guess it would have helped if I had added document. to the request?!

 

Anyways hers what finally did the trick:-

 


var oldEvent = (window.onload) ? window.onload : function () {};
window.onload = setTimeout( function () {
       oldEvent();
               document.getElementById("btnConfirm").onclick = null;
               }, 1000);	

 

 

Thanks to both for the help and suggestions ;)

  • Jo 90 locked this topic
Guest
This topic is now closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.