0

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>
Carlos
  • 6,805
  • 22
  • 83
  • 171

3 Answers3

5

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')

Brad M
  • 7,689
  • 1
  • 19
  • 37
2

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);
        });
    })
})
Arun P Johny
  • 365,836
  • 60
  • 503
  • 504
-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>
Bart
  • 18,467
  • 7
  • 66
  • 73
  • 2
    If you do copy content, please have the decency to at least give proper attribution: http://forum.jquery.com/topic/click-not-working-for-ie7-8 – Bart Jun 27 '13 at 15:24