lucasjohnson lucasjohnson - 10 months ago 23
Javascript Question

Query whether an element has more than 1 ID, if so, join them and place them back as 1 id

I'm trying to use either jQuery or JS to find out if an element has more than one id, then if it does, join them together with a '-', then reinsert it back into that id as one. Please see the code:

<script src=""></script>

<a id="Join This" href=""></a>

<!-- result -->
<a id="join-this" href=""></a>

Thanks in advance!


You could grab all the elements on the page then loop through them, making sure the id has a - in place of spaces.

var els = document.getElementsByTagName('*')
for(var i = 0, l = els.length; i < l; i++) {
    els[i].id = els[i].id.split(' ').join('-');

Example here.