Considering an id ("identifier") is (should be) unique, why not just use the most specific identifier you have -- the second one :
$('d4')
If your identifiers are not unique, they are not identifiers... And they shouldn't be used as the "id" attribute of your divs...
Actually, if you have something like this :
p1
d1
d2
p2
d1
d2
d3
p3
d1
Your "pX" can be id (they are unique), but your "dX" shouldn't be ids ; a solution might be to use the class to store the "dX" information.
A bit like this, I suppose :
id=p1
class=d1
class=d2
id=p2
class=d1
class=d2
class=d3
id=p3
class=d1
Then, you'll use the $$ function with a CSS-selector : if you want the element with a class="d2" inside the elemnt of id="p1", something like this might work :
$$('#p1 .d2');
ie :
- #p1 = element with id=p1
- .d2 = element which has a class containing d2
Hope this is clear and helps...