DEV Community

Cover image for JavaScript: Use the first Option of a Select as a Label

JavaScript: Use the first Option of a Select as a Label

Simon Köhler
Freelance PHP Backend-Developer and Expert for TYPO3 Open Source CMS
Updated on ・2 min read

Sometimes input or select elements are used in forms without <label> tag. When the user selects an option in the select menu, by default he sees only the label of the selected item.

Simple Example

    <option selected>CATEGORY</option>
Enter fullscreen mode Exit fullscreen mode

If you select the option "2" in the above example, only a 2 will be displayed in the select. Wouldn't it be cool if it said "CATEGORY: 2" instead?

So it would look like this but ONLY when you select an entry!

    <option selected>CATEGORY: 1</option>
Enter fullscreen mode Exit fullscreen mode

How about simply writing the first option of the select menu as "label" in front of the option?


    // Find all selects in the current DOM
    let selects = document.querySelectorAll('.inline-label');

        selects.forEach(select => {
            let options = select.querySelectorAll('option');
            let firstOption = options[0];
            options.forEach(option => {
                // Set a data attribute for each option with the original label text for later
                // When the select has changed, do the magic and add the label prefix
                let selectedOption = select.querySelector('option[value="'+select.value+'"]');
                // But before, reset the options to make sure only the new selected option has a label
                selectedOption.innerHTML = firstOption.innerHTML +': '+ selectedOption.innerHTML;

    let resetOptions = function(options){
        options.forEach(option => {
            // Set the inner HTML back to the original value stored in the data attribute
            option.innerHTML = option.getAttribute('data-original');

Enter fullscreen mode Exit fullscreen mode

Here's the Code on

Not perfect: Things to do

If an entry has already been selected and you open the menu again, but then do not select a new entry, the label disappears. For this case, a solution still needs to be integrated. Maybe you have an idea? Then don't hesitate to edit the CodePen and present your solution here!

NOTE: @crys_dev helped to fix it, see comments below!

You might be interested in this...

Support my work:

Follow me:

Learn TYPO3 with the Video Training for Version 9 LTS of Wolfgang Wagner:

Your TYPO3 Developer & Freelancer - let's start a great TYPO3 project together!

TYPO3 Slug Editor for SEO Gurus:

TYPO3 Timeline Extension for frontend timelines:

TYPO3 Font Awesome Extension for awesome icons:

USA Hosting with Bluehost:

Island Hosting with Orange Website:

Discussion (2)

crys_dev profile image
Vincent Rouilhac

Nice post ! To solve your issue I just put the event listener that calls resetOptions to not call it so the value is not reset when you click again.

typo3freelancer profile image
Simon Köhler Author

You are right, somehow this one was not really needed. Well I did this whole thing in 15 minutes or so ;-) Thank you buddy I have changed the code!