Contact Us

Let us know to help you out quickly!

 Admin User     Feb,08 2017

Overwrite old click event function Not Working in IE

I just came across this problem that is happening in IE. In the html I am adding an onclick event to an element that calls a function 'changeStatus' like this :

<a id="my_a" onclick="changestatus('my_a',1);" href="javascript:void(0);">Click Here</a> 

function changestatus(id,status){alert(status);jQuery('#'+id).html('Yes');jQuery('#'+id).unbind('click').removeAttr('onClick');jQuery('#'+id).click(function(event){changestatus(id,0);});}

Inside the changeStatus function I added the .unbind('click').removeAttr('onClick'); in order to remove the old onclick event and then add the new one.

This is working fine in FF, but in IE it is calling both the new click event and the old at the same time for some reason. I think that this causes jQuery to not know what events are taking place elsewhere, so both events get run.
When I put the following code and just bind the click event with jquery on document.ready and not set the onclick attribute of the anchor tag.

<a id="my_a" href="javascript:void(0);" >Click Here</a>

jQuery(document).ready(function(){$('#my_a').click(function() {changestatus('my_a',1);});});

This is works well on both FF and IE and I believe that this is the best way to fix this problem.
Please correct me if this is wrong logic or any other way to fix this problem.