문제

We are using the following to dynamically change an element's id:

$('.next').attr('id','next'+parseInt(Number(id_next)-1));

For example, #next1 becomes #next0.

After changing the id, jQuery is no longer able to find the id #next0:

if($('#next0').length>0){
    $('.previous').css({'visibility' : 'visible'});
    $('.next').css({'visibility' : 'hidden'});
}

Is parseInt causing a problem here?

Thanks in advance for any help!

도움이 되었습니까?

해결책

No parseInt is not causing a problem here.

Here's a fiddle to demonstrate that the code works: http://jsfiddle.net/sZXJ8/

HTML:

<div class="next" id="next1"></div>

JS:

var id_next = 1;
$('.next').attr('id','next'+parseInt(Number(id_next)-1), 10); // Always include a radix when using parseInt
alert($('#next0').length);

Your problem is elsewhere.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top