The HTML DOM console.groupCollapsed() method specifies the beginning of a collapsed message group.
Syntax
Following is the syntax −
console.groupCollapsed(label)
Here, label is the label for the group.
Example
Let us look at an example for the console.groupCollapsed() method −
<!DOCTYPE html> <html> <body> <h1>console.groupCollapsed() Method</h1> <p>Press F12 key to view the message in the console view.</p> <button type="button" onclick="normMessage>NORMAL</button> <button type="button" onclick="CollMessage()">COLLAPSED</button> <script> function normMessage(){ console.log("Hello world!"); } function CollMessage(){ console.groupCollapsed(); console.log("This message will be inside a collapsed group!"); console.log("This message will also be inside a collapsed group!"); } </script> </body> </html>
Output
This will produce the following output −
On clicking the NORMAL button and looking at the console view in developer options −
On clicking the COLLAPSED button and looking at the console view in developer options −
On clicking the COLLAPSED button and looking at the console view in developer options −
In the above example −
We have created two buttons COLLAPSED and GROUP that will execute the CollMessage() and groupMessage() method upon being clicked by the user.
<button type="button" onclick="normMessage()">NORMAL</button> <button type="button" onclick="CollMessage()">COLLAPSED</button>
The normMessage() method has the console.log() method that takes in a string or object that is supplied as a parameter and simply displays it to the console.
function normMessage(){ console.log("Hello world!"); }
The CollMessage() method has the console.groupCollapsed() method inside it that states that all of the messages that are written after this point will be displayed inside a collapsed message group. The messages are now collapsed inside the message group unlike the default extended view of the group() method −
function CollMessage(){ console.groupCollapsed(); console.log("This message will be inside a collapsed group!"); console.log("This message will also be inside a collapsed group!"); }