javascript - Redirecting after chosing an option in select tag - Stack Overflow

I bring you a question about a <select> tag I am working for a website. I believe that the code I

I bring you a question about a <select> tag I am working for a website. I believe that the code I am writing here should be okay, but considering that I am quite new to js (I mainly know the basics of html and css, though it has been a while since I have worked with front-end web design :p), it is not surprising that it isn't working.

Basically, I have a <select> tag with different language options, and when clicking on one, you should be redirected to the page in the language you have selected. My attempt, with the little knowledge I have, was to get the info of the select tag, retrieving the value in the var direction to then open the link to the desired href.

Here is the code of the html (I post here only the header of basic_en.html, where the <select> is found):

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> 

        <link rel="stylesheet" href="+Symbols+Outlined" />
        <link rel="stylesheet" href="+Symbols+Outlined" />

        <link rel="stylesheet" href="style.css">
        <script src="app.js"></script>
        <title>Website</title>
    </head>
    <body>
        <div class="header">
            <nav>
            <ul class="sidebar">
                <li onclick=hideSidebar() class="item-nav">
                    <a>
                        <span class="material-symbols-outlined">
                            close
                        </span>
                    </a>
                </li>
                <li class="item-nav">
                    <h4><a href="basic_en.html">Home</a></h4>
                </li>
                <li class="item-nav">
                    <h4><a href="#">CV</a></h4>
                </li>
                <li class="item-nav">
                    <h4><a href="#">Contact</a></h4>
                </li>
                <li class="item-nav select-container">
                    <select id="lang-box">
                        <option class="item-lang" value="basic_en.html" onclick=changeLang()>EN</option>
                        <option class="item-lang" value="basic_es.html" onclick=changeLang()>ES</option>
                        <option class="item-lang" value="basic_fr.html" onclick=changeLang()>FR</option>
                    </select>
                </li>
            </ul>
            <ul class="nav">
                <li class="item-nav">
                    <h4><a href="basic_en.html">Name Surname</a></h4>
                </li>
                <li class="item-nav hide-maxwidth">
                    <h4><a href="#">CV</a></h4>
                </li>
                <li class="item-nav hide-maxwidth">
                    <h4><a href="#">Contact</a></h4>
                </li>
                <li class="item-nav select-container hide-maxwidth">
                    <select id="lang-box">
                        <option class="item-lang" value="basic_en.html" onclick=changeLang()>EN</option>
                        <option class="item-lang" value="basic_es.html" onclick=changeLang()>ES</option>
                        <option class="item-lang" value="basic_fr.html" onclick=changeLang()>FR</option>
                    </select>
                </li>
                <li onclick=showSidebar() class="item-nav menu-btn">
                    <a>
                        <span class="material-symbols-outlined">
                            menu
                        </span>
                    </a>
                </li>
            </ul>
            </nav>
        </div>

The CSS, the document style.css:

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.header{
    color: white;
    width: 100%;
}
nav{
    background-color: black;
    width: 100%;
    height: 6.5rem;
    position: fixed;
    z-index: 68;
}
.nav{
    display:flex;
    justify-content: flex-end;
    align-items: center;
    width: 100%;
}
.item-nav{
    list-style: none;
    margin-right: 2rem;
    font-size: 1.25rem;
}
.item-nav a{
    text-decoration: none;
    color: white;
}
.item-nav:first-child{
    margin-right: auto;
    margin-left: 2rem;
    font-size: 4rem;
}
.sidebar{
    position: fixed;
    top: 0;
    right: 0;
    height: 100dvh;
    height: 100vh;
    width: 180px;
    z-index: 69;
    background-color: black;
    color: white;
    display: none;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
    gap: 1rem;
    float: right;
    padding: 1rem;
}
.sidebar ul{
    width: auto;
}
.menu-btn{
    display: none;
}
.select-container{
    position: relative;
    width: 2rem;
}
#lang-box{
    border: none;
    appearance: none;
    width: 100%;
    color: white;
    background-color: black;
    cursor: pointer;
    font-family: Cabin;
    font-size: 1.25rem;
}
.item-lang{
    background-color: black;
    border-radius: 0;
}
@media only screen and (max-width: 930px){
    .hide-maxwidth{
        display: none;
    }
    .menu-btn{
        display: block;
    }
}

And the little JS involved in app.js:

/* Here is where I tried the redirection */
function changeLang(){
    var select = document.getElementById('lang-box');
    var direction = select.options[select.selectedIndex].value;
    window.location.href = direction;
}
/* Functionality of sidebar (and yes, I know it does not close when the viewport is too big, I am working on it hahaha)*/
function showSidebar(){
    const sidebar = document.querySelector('.sidebar');
    sidebar.style.display = 'flex';
}
function hideSidebar(){
    const sidebar = document.querySelector('.sidebar');
    sidebar.style.display = 'none';
}

If you could indicate me what is wrong as well as any further advice to improve on JS (or in css as well), I would greatly appreciate it. Thanks in advance :)

I bring you a question about a <select> tag I am working for a website. I believe that the code I am writing here should be okay, but considering that I am quite new to js (I mainly know the basics of html and css, though it has been a while since I have worked with front-end web design :p), it is not surprising that it isn't working.

Basically, I have a <select> tag with different language options, and when clicking on one, you should be redirected to the page in the language you have selected. My attempt, with the little knowledge I have, was to get the info of the select tag, retrieving the value in the var direction to then open the link to the desired href.

Here is the code of the html (I post here only the header of basic_en.html, where the <select> is found):

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0"> 

        <link rel="stylesheet" href="https://fonts.googleapis/icon?family=Material+Symbols+Outlined" />
        <link rel="stylesheet" href="https://fonts.googleapis/icon?family=Material+Symbols+Outlined" />

        <link rel="stylesheet" href="style.css">
        <script src="app.js"></script>
        <title>Website</title>
    </head>
    <body>
        <div class="header">
            <nav>
            <ul class="sidebar">
                <li onclick=hideSidebar() class="item-nav">
                    <a>
                        <span class="material-symbols-outlined">
                            close
                        </span>
                    </a>
                </li>
                <li class="item-nav">
                    <h4><a href="basic_en.html">Home</a></h4>
                </li>
                <li class="item-nav">
                    <h4><a href="#">CV</a></h4>
                </li>
                <li class="item-nav">
                    <h4><a href="#">Contact</a></h4>
                </li>
                <li class="item-nav select-container">
                    <select id="lang-box">
                        <option class="item-lang" value="basic_en.html" onclick=changeLang()>EN</option>
                        <option class="item-lang" value="basic_es.html" onclick=changeLang()>ES</option>
                        <option class="item-lang" value="basic_fr.html" onclick=changeLang()>FR</option>
                    </select>
                </li>
            </ul>
            <ul class="nav">
                <li class="item-nav">
                    <h4><a href="basic_en.html">Name Surname</a></h4>
                </li>
                <li class="item-nav hide-maxwidth">
                    <h4><a href="#">CV</a></h4>
                </li>
                <li class="item-nav hide-maxwidth">
                    <h4><a href="#">Contact</a></h4>
                </li>
                <li class="item-nav select-container hide-maxwidth">
                    <select id="lang-box">
                        <option class="item-lang" value="basic_en.html" onclick=changeLang()>EN</option>
                        <option class="item-lang" value="basic_es.html" onclick=changeLang()>ES</option>
                        <option class="item-lang" value="basic_fr.html" onclick=changeLang()>FR</option>
                    </select>
                </li>
                <li onclick=showSidebar() class="item-nav menu-btn">
                    <a>
                        <span class="material-symbols-outlined">
                            menu
                        </span>
                    </a>
                </li>
            </ul>
            </nav>
        </div>

The CSS, the document style.css:

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
.header{
    color: white;
    width: 100%;
}
nav{
    background-color: black;
    width: 100%;
    height: 6.5rem;
    position: fixed;
    z-index: 68;
}
.nav{
    display:flex;
    justify-content: flex-end;
    align-items: center;
    width: 100%;
}
.item-nav{
    list-style: none;
    margin-right: 2rem;
    font-size: 1.25rem;
}
.item-nav a{
    text-decoration: none;
    color: white;
}
.item-nav:first-child{
    margin-right: auto;
    margin-left: 2rem;
    font-size: 4rem;
}
.sidebar{
    position: fixed;
    top: 0;
    right: 0;
    height: 100dvh;
    height: 100vh;
    width: 180px;
    z-index: 69;
    background-color: black;
    color: white;
    display: none;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
    gap: 1rem;
    float: right;
    padding: 1rem;
}
.sidebar ul{
    width: auto;
}
.menu-btn{
    display: none;
}
.select-container{
    position: relative;
    width: 2rem;
}
#lang-box{
    border: none;
    appearance: none;
    width: 100%;
    color: white;
    background-color: black;
    cursor: pointer;
    font-family: Cabin;
    font-size: 1.25rem;
}
.item-lang{
    background-color: black;
    border-radius: 0;
}
@media only screen and (max-width: 930px){
    .hide-maxwidth{
        display: none;
    }
    .menu-btn{
        display: block;
    }
}

And the little JS involved in app.js:

/* Here is where I tried the redirection */
function changeLang(){
    var select = document.getElementById('lang-box');
    var direction = select.options[select.selectedIndex].value;
    window.location.href = direction;
}
/* Functionality of sidebar (and yes, I know it does not close when the viewport is too big, I am working on it hahaha)*/
function showSidebar(){
    const sidebar = document.querySelector('.sidebar');
    sidebar.style.display = 'flex';
}
function hideSidebar(){
    const sidebar = document.querySelector('.sidebar');
    sidebar.style.display = 'none';
}

If you could indicate me what is wrong as well as any further advice to improve on JS (or in css as well), I would greatly appreciate it. Thanks in advance :)

Share Improve this question asked Mar 2 at 21:48 Th3W31rd0Th3W31rd0 1138 bronze badges 2
  • 1 As an aside, select.options[select.selectedIndex].value is completely unnecessary; just use select.value. – Unmitigated Commented Mar 2 at 22:47
  • @Unmitigated, thanks for the tip, I did the change (plus the change advised by @UmairSafraz) and it is kind of working, but now a new problem arised regarding the item is being selected even before I can click on another option. Check Umair's response and the discussion I opened up (I do not know how follow-up questions work... It is posted in the discussion, under the tag of [javascript]) – Th3W31rd0 Commented Mar 3 at 9:59
Add a comment  | 

2 Answers 2

Reset to default 1

Remove onclick from the <option> elements and add onchange to the <select> element.

<select id="lang-box" onchange="changeLang()">
    <option value="basic_en.html">EN</option>
    <option value="basic_es.html">ES</option>
    <option value="basic_fr.html">FR</option>
</select>

JavaScript

function changeLang() {
    var select = document.getElementById('lang-box');
    var direction = select.options[select.selectedIndex].value;
    window.location.href = direction;
}

After a bit of bashing my head against the wall and giving it some rest, I decided to just put everything in the onchange of the html and work with the value obtained from the select itself (this.value) instead of retrieving it to the app.js since I could not figure out for the life of me a way of doing it with the aforementioned changeLang() function in js file.

var direction = this.value; 
this.value = this.options[0].value; 
location = direction;

It is a bit lazy, but it is a good workaround. Thanks to @UmairSarfraz for the response (I did not realize the thing with the onchange attribute) since it helped me to get to a solution.

PS: Turns out there was indeed someone with a problem the same as mine but I did not find the post at the time of writing this question. Shoutout to the response of @TJCrowder, because not only did he gave the same answer, but also it gave a way of doing that function with an EventListener looking for changes. You can find more details clicking this link

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745117245a4612201.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信