Best way to change navbar dropdown function from CSS-only to Javascript on mobile?

Here is my current CSS-only dropdown menu html:

        <div class="main-nav">
            <div class="main-nav-container">
                <div class="nav-links">
                    <ul>
                        <li class="nav-link"><a>Nav Link</a>
                            <div class="dropdown">
                                <ul class="dropdown-list-type">
                                    <li>
                                        <ul>
                                            <li>
                                                <a href="#">Option</a>
                                            </li>
                                            <li>
                                                <a href="#">Option</a>
                                            </li>
                                            <li>
                                                <a href="#">Option</a>
                                            </li>
                                        </ul>
                                    </li>                                
                                </ul>
                            </div>
                        </li>
                     
                        <li class="nav-link"><a>Nav Link</a>
                            <div class="dropdown">
                                <ul class="dropdown-list-type">
                                    <li>
                                        <ul>
                                            <li>
                                                <a href="#">Option</a>
                                            </li>
                                            <li>
                                                <a href="#">Option</a>
                                            </li>
                                            <li>
                                                <a href="#">Option</a>
                                            </li>
                                        </ul>                                        
                                    </li>
                                </ul>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

Relevant CSS:

.dropdown{
position: absolute;
display: flex;
top: 100%;
border-top: solid 1px var(--white-3);
left: 0;
z-index: 8;
opacity: 0;
pointer-events: none;
transition: .3s; }

.nav-link:hover > .dropdown,
.dropdown-link:hover > .dropdown{
    transform: translate(0, 0);
    opacity: 1;
    pointer-events: auto;
}

On mobile, I want the user to be able to open and close the dropdown by tapping on the “Nav Link”. Currently, the user can tap to open, but then has to tap somewhere else to close the dropdown. I figure I need Javascript make it do what I want.

My idea:

  • Use a media query to remove the hover function on mobile
  • Use Javascript to add a class to the “Nav Links” on mobile
  • Using this class, with JS, make the Nav Links toggle the dropdown to display/hide

Is this the best way to do it? If so, how do I add a class to the “Nav Links” with Javascript at a specific screen size?

I would like to just use plain Javascript, no Jquery.

Also, I current want to keep the CSS-only hover approach for desktop. So I want the Javascript function only for the mobile view.

I hope that makes sense to everyone. Thank you!