Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


                <h1>Vanilla JavaScript JSONify <code>&lt;select&gt;</code> Element</h1>
<p>Converts the regular <code>&lt;select&gt;</code> element into JSON. Opinionated. Light. ES6.</p>

<p><a href="">JSONify Dropdown Element Using Vanilla JavaScript</a></p>

    <li>onclick convert select to indented ul/li including optgroups</li>
    <li>onclick JSONify select to console.log()</li>

<div id="myselect">
    <select id="multiselect" name="languages[]" size="6" multiple>
        <optgroup label="More Cars">
            <option value="1">JavaScript</option>
            <option value="2">CSS</option>
            <option value="3">Script</option>
            <option value="4">Ruby</option>
            <option value="5">Go</option>
            <option value="6">PHP</option>
            <option value="7">.Com</option>
            <option value="8">Java</option>
        <optgroup label="Swedish Cars">
            <option value="volvo">Volvo</option>
            <option value="saab">Saab</option>
        <optgroup label="German Cars">
            <option value="mercedes">Mercedes</option>
            <option value="audi">Audi</option>
    <div id="json"></div>




                function jsonifySelect(selectId) {
    const theSelect = document.getElementById('multiselect'),
          optgroups = theSelect.getElementsByTagName('optgroup');
    let myObj = {};
    for (let i = 0; i < optgroups.length; i++) {
        myObj[optgroups[i].getAttribute('label')] = [];
        let options = optgroups[i].getElementsByTagName('option');
        for (let j=0; j < options.length; j++) {

    return myObj;

function recurse(data) {
    let htmlRetStr = '<ul>';

    for (let key in data) {
        if (typeof(data[key]) == 'object' && data[key] != null) {
            var x = key*1;

            if (isNaN(x)) {
                htmlRetStr += "<li>" + key + "<ul>";
            htmlRetStr += recurse(data[key]);
            htmlRetStr += '</ul></li>';
        } else {
            htmlRetStr += ('<li data-value="' + data[key] + '">' + data[key] + '</li>');
  htmlRetStr += '</ul>';
  return htmlRetStr;

document.addEventListener('DOMContentLoaded', () => {
    document.getElementById('myselect').addEventListener('click', () => {

        var htmlStr = recurse(jsonifySelect('multiselect'));
        document.getElementById('json').innerHTML = htmlStr;