AndrewS
AndrewS

Reputation: 1565

jquery click() not working in ie7 and ie8

Can some one help to solve this:

$('#cont2, #cont3').hide();
$('li a').click(function(){
    var idx= $(this).attr('id').substr(-1);
    $('#cont1, #cont2, #cont3').hide();
    $('#cont'+idx).show();
    return false;
});

This is not working in IE7 and IE8. Why?

HTML

<ul>
       <li class="linktab"><a href="#" id="link1">Link1</a></li>
       <li class="linktab"><a href="#" id="link2">Link2</a></li>
       <li class="linktab"><a href="#" id="link3">Link3</a></li>
</ul>

<div id="cont1">content 1</div>
<div id="cont2">content 2</div>
<div id="cont3">content 3</div>

Upvotes: 0

Views: 226

Answers (1)

Jon
Jon

Reputation: 437376

Because old IE's substr implementation does not support a negative index as the argument. There is a drop-in replacement in that MDN page, or you can simply do

var id = $(this).attr('id');
var idx= id.substr(id.length - 2);

Upvotes: 4

Related Questions