Print an array into an HTML element in JavaScript
Printing an array into an HTML element in JavaScript.
The text content of the page heading is divided into substrings by blank space, put into an array and the array is printed into a div:
HTML code:
<div class="example"></div>
JavaScript code:
<script>
var array = new Array();
array = document.querySelector('h1').textContent.split(' ');
array.forEach( function( element, index ) {
document.querySelector('.example').innerHTML += '<b>[ ' + index + ' ] </b>' + element + '<br />';
});
</script>
Browser support
- Windows
- Edge 12.0+
- Firefox 3.5+
- Google Chrome 1.0+
- Opera 10.0+
- Linux
- Firefox 3.5+
- Google Chrome 1.0+
- Opera 10.0+
- iOS
- Safari 2.0+
- Android
- Samsung Internet 1.0+
- Chrome Android 18.0+
- Firefox for Android 4.0+
- Opera Android 10.1+