Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top