unique ID for the component in CQ5

By Erum Karim via flickr

If you have to deal with javascript, divs and component you could end in the problem that unique Id in a component page is not so unique. If you for example have an html/javascript like

<div id="myDiv"></div>
<script type="text/javascript">document.getElementById("myDiv").innertHTML="Hello World!";</script>

will work fine if you include your component only once in your page. But if you include it more than that you’ll get some unpredictable behaviors, but for sure it wont work.

In order to solve the problem, you can for example use the currentNode in the jsp using his getIdentifier() method.

<div id="<%=currentNode.getIdentifier()%>"></div>
<script type="text/javascript">document.getElementById("<%=currentNode.getIdentifier()%>").innertHTML="Hello World!";</script>

Done, done!