How to condense multiple "document.getElementById" into one statement?

<script type="text/javascript">
document.getElementById('collapse').innerHTML = 'Collapse all';
document.getElementById('expand').innerHTML = 'Expand all';
document.getElementById('bar').innerHTML = '|';
</script>

Thanks :slight_smile:

With what you’re doing above you can’t as there would be no way to set the HTML for each individual element if they would grouped together.

The best you could do to shorten this code would be to create a function so that you can remove the common part of each statement.

set = function (id, txt) {document.getElementById(id).innerHTML = txt;}
set('collapse','Collapse all');
set('expand','Expand all');
set('bar', '|');

Thanks Felgall. Is one way faster than the other? Negligible I know. It’s wins on code length.

document.getElementById(‘collapse’).innerHTML = ‘Collapseall’;document.getElementById(‘expand’).innerHTML = ‘Expandall’;document.getElementById(‘bar’).innerHTML = ‘|’;

set = function (id, txt) {document.getElementById(id).innerHTML = txt;}set(‘collapse’,‘Collapseall’);set(‘expand’,‘Expandall’);set(‘bar’, ‘|’);