JavaScript/hacks: Difference between revisions

From Freephile Wiki
Created page with "A collection of little JavaScript hacks. == Pull headings out of a page == <syntaxhighlight lang="javascript">Array.from(document.getElementsByTagName("h1")).forEach(function (item) { console.log(item.textContent); });</syntaxhighlight>Using the browser's developer console, you can pop this one-liner in to see the list of '''headings''' in your web page. * <code>document.getElementsByTagName("h1")</code> returns an [https://developer.mozilla.org/en-US/docs/Web/API/HTML..."
 
Add bigger example
Line 1: Line 1:
A collection of little JavaScript hacks.
A collection of little JavaScript hacks.
Note that you can play with JavaScript at Mozilla's [https://developer.mozilla.org/en-US/play MDN Playground] or more commercial options like [https://jsfiddle.net/ JSFiddle]


== Pull headings out of a page ==
== Pull headings out of a page ==
Line 8: Line 10:
* The forEach loop creates an anonymous function to handle each <code>item</code> in the array.
* The forEach loop creates an anonymous function to handle each <code>item</code> in the array.
* We <code>console.log()</code> it to see the values; and use the <code>textContent</code> property instead of <code>innerHTML</code> since the latter would almost certainly be 'messy'. (In other words, innerHTML would contain far more than what we want - which is just the text of the headings).
* We <code>console.log()</code> it to see the values; and use the <code>textContent</code> property instead of <code>innerHTML</code> since the latter would almost certainly be 'messy'. (In other words, innerHTML would contain far more than what we want - which is just the text of the headings).
 
A little different approach would be to build up the text content instead of dumping it to the console on each iteration<syntaxhighlight lang="javascript">
const headings = document.getElementsByTagName("h3");
let result = '';
for (let i = 0; i < headings.length; i++) { 
    result += `\n ${headings[i].textContent }`;
   
}
console.log(result);
</syntaxhighlight>
[[Category:JavaScript]]
[[Category:JavaScript]]

Revision as of 12:30, 31 January 2025

A collection of little JavaScript hacks.

Note that you can play with JavaScript at Mozilla's MDN Playground or more commercial options like JSFiddle

Pull headings out of a page

Array.from(document.getElementsByTagName("h1")).forEach(function (item) { console.log(item.textContent); });

Using the browser's developer console, you can pop this one-liner in to see the list of headings in your web page.

  • document.getElementsByTagName("h1") returns an HTMLCollection from the DOM.
  • The Array.from() converts the HTMLCollection NodeList into an actual array.
  • The forEach loop creates an anonymous function to handle each item in the array.
  • We console.log() it to see the values; and use the textContent property instead of innerHTML since the latter would almost certainly be 'messy'. (In other words, innerHTML would contain far more than what we want - which is just the text of the headings).

A little different approach would be to build up the text content instead of dumping it to the console on each iteration

const headings = document.getElementsByTagName("h3");
let result = ''; 
for (let i = 0; i < headings.length; i++) {  
    result += `\n ${headings[i].textContent }`; 
    
} 
console.log(result);