How to return Element ID instead of [object HTMLDivElement] (Prototype.js)

Dave picture Dave · Feb 23, 2009 · Viewed 56.2k times · Source

I know this is simple, but I can't wrap my head around it. Currently the following code returns "[object HTMLDivElement],[object HTMLDivElement]" I'd like it to return "div1,div2". Any thoughts? Thanks guys.

<HTML>
<HEAD>
<script type="text/javascript" src="path_to/prototype.js"></script>
<script>
function linkClick ()
{
    alert($$('div.basic'));
}
</script>
</HEAD>
<BODY>
   <div id="div1" class="basic" onclick="linkClick();"></div>
   <div id="div2" class="basic" onclick="linkClick();"></div>
</BODY>
</HTML>

Answer

Konrad picture Konrad · Mar 6, 2009

I had the same problem, there is a simple solution :

If varElement is your object containing a [HTMLDivElement] (or other DOM object) simply add '.id'

e.g.

varElement.id