Reputation: 7971
I want to add one function dynamically on a tag which is working fine on all browser except IE7.
<head>
<script type="text/javascript" src="jquery-1.8.3.js"></script>
<script type="text/javascript">
function show(num){
if(num==1){
$('#one').removeAttr('onclick')
$('#two').attr('onclick','show(1)')
alert(0)
}}
</script>
</head>
<body>
<a href="javascript:void(0)" onclick="show(1)" id="one">one</a>
<a href="javascript:void(0)" id="two">two</a>
</body>
</html>
Upvotes: 0
Views: 1285
Reputation: 3
As taken from http://forum.jquery.com/topic/click-not-working-for-ie7-8
I have a dead simple .click() event setup for a div with the id of nextLink. This works perfectly in all the "real" browsers.
$('#nextLink').click(function () { alert("nextLink here"); }); <div id="nextLink">Next</div>
Upvotes: -3
Reputation: 388316
Since you are using jQuery, use it to register the event handlers instead of using onclick
attribute
You can use .one() event register since you want to call the event handler only once instead of using .on() and .off()
jQuery(function(){
function handler(num){
alert(num)
}
$('#one').one('click', function(){
handler(1);
$('#two').on('click', function(){
handler(1);
});
})
})
Upvotes: 2
Reputation: 7898
This is not the correct way to attach/detach event handlers. You can try using this methodology instead.
$('#one').off('click');
$('#two').click(function() { show(1); });
.click()
is a wrapper function for .on('click')
Upvotes: 5