Click here to Skip to main content
15,888,014 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to make a dropdown menu in html5 using the tag to select 2 options: if the user is male or female and than, if the user is a male i want to excecute a statement like write on html "You're male" when a button is pressed and if the user is female the script will write "You're female", but it doesn't work.

What I have tried:

Moved to correct box

What I have tried:

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form>
        <select name="select_menu">
            <option value="select-gender">- Select your gender -</option>
            <option value="male">Male</option>
            <option value="female">Female</option>
        </select>
        <button onclick="gender()">Print</button>
    </form>
    <script>
        function gender(){
            var elements = document.getElementsByName("select_menu").value;
            if(elements == select-gender) {
                alert("Select your gender");
            }
            if(elements == male) {
                document.write("You're male");
            }
            if(elements == female){
                document.write("You're female");
            }
        }   
    </script>
</body>
</html>
Posted
Updated 17-Jun-22 22:09pm
v2
Comments
Member 15627495 18-Jun-22 5:31am    
the 'onchange' event handler is made for you.

https://www.w3schools.com/jsref/event_onchange.asp
Richard MacCutchan 18-Jun-22 6:26am    
You should post that as a Solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900