Jump to content

change image with javascript

Go to solution Solved by elpiop,

Are you sure the path to the image is correct?

hey guys so I'm trying to change a photo written in as a CSS background photo example below but when I click the button to activate it the button just goes blank, any help? thanks!

/* html Stuff */
<div id="img_main">
    
    </div>
/* Css Stuff */
#img_main{
    text-align: center;
    background-color: rgba(255, 255, 255, 0.1);;
    grid-column: 1/10;
    grid-row: 1/6;
    transition: .5s;
    height: 800px;
    overflow: hidden;
    background-image: url("../images/temp.png");
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    
}
/* java stuff */
function nextImg() {
document.getElementById("img_main").style.backgroundImage = "url('../images/nature.jpg')";
}

EDIT: here's a visual of what happens https://gyazo.com/7c641edb3a0a97988ae657c500f15405

Link to comment
https://linustechtips.com/topic/1092338-change-image-with-javascript/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×