Friday, 30 October 2020

How do I change the background of a button when clicked?

Okay, okay. I know many people have asked this question on Stack Overflow, but the solutions don't work for me. So my problem is simple: how do I make the female-av-button and male-av-button have a background URL of female-avatar & male-avatar respectively? Here's my code:

body{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    background-color: black;
}

.avatars{
    justify-content: center;
    margin-left: 15%;
    display: flex;
}

.choose-a-user-text{
    font-family: 'Luckiest Guy';
    font-size: 400%;
    justify-content: center;
}

.choose-a-username{
    margin-left: 25%;
}

.user-input{
    margin-left: 29%;
}

.user-input:focus{
    outline: none;
}

.female-av-button{
    background: none;
    border: none;
    padding: 1px;
}

.female-av-button:focus{

}

.male-av-button{
    background: none;
    border: none;
    padding: 1px;
}

.female-av{
    background: url('../img/female-avatar-silhouette.png') no-repeat;
    width: 500px;
    height: 700px;
}

.female-av:hover{
    background: url('../img/female-avatar.png') no-repeat;
    width: 500px;
    height: 700px;
}

.male-av{
    background: url("../img/male-avatar-silhouette.png") no-repeat;
    width: 500px;
    height: 700px;
}

.male-av:hover{
    background: url("../img/male-avatar.png") no-repeat;
    width: 500px;
    height: 700px;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <meta charset="utf-8">
        <title>Choose Your Character</title>
        <link rel="stylesheet" href="css/avatar-page.css">
        <link href="https://fonts.googleapis.com/css2?family=Luckiest+Guy&display=swap" rel="stylesheet">
    </head>
    <body>
        <div class="choose-a-username">
            <h2 class="choose-a-user-text" style="color: #018D94;">CHOOSE A USERNAME</h2>
            <input class="user-input" type="text" name="" value="" placeholder="username">
        </div>
        <div class="avatars">
            <button type="button" onclick="chooseanav()" class="female-av-button" name="button"><div class="female-av"></div></button>
            <button type="button" class="male-av-button" name="button"><div class="male-av"></div></button>
        </div>


        <!-- <div class="avatars">
            <div class="silhos">
                <img src="img/male-avatar-silhouette.png" class="avatar-silho" alt="male avatar silho">
                <img src="img/female-avatar-silhouette.png" class="avatar-silho" alt="female avatar silho">
            </div>
            <div class="avas">
                <img src="img/male-avatar.png" class="avatar" alt="male avatar">
                <img src="img/female-avatar.png" class="avatar" alt="female avatar">
            </div>
        </div> -->
        <script type="text/javascript">
            // document.getElementsByClassName("user-input").style.height="500px";

            function chooseanav() {
                document.getElementsByClassName('female-av').style.background = "url('../img/female-avatar.png') no-repeat";
            }

        </script>
    </body>
</html>

Any help is greatly appreciated. Thanks!



from How do I change the background of a button when clicked?

No comments:

Post a Comment