- February 8, 2017
- Posted by: user
- Category: Uncategorized
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 :
[code:xml]<a id="my_a" onclick="changestatus(‘my_a’,1);" href="javascript:void(0);">Click Here</a> [/code]
[code:js]
function changestatus(id,status)
{
alert(status);
jQuery(‘#’+id).html(‘Yes’);
jQuery(‘#’+id).unbind(‘click’).removeAttr(‘onClick’);
jQuery(‘#’+id).click(function(event){changestatus(id,0);});
}
[/code]
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.
[code:xml]<a id="my_a" href="javascript:void(0);" >Click Here</a>[/code]
[code:js]jQuery(document).ready(function(){
$(‘#my_a’).click(function() {
changestatus(‘my_a’,1);
});
});[/code]
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.