0

I have a button that I want to change the current background but can not figure out how to make javascript know what my class is for my background picture which is set in css. I found a way by changing the class to just header but thats not really what I wanna do. sry for sloppy coding.

<script type="text/javascript">
    function ps() {
        var bg = document.getElementsByTagName('headBack')[0];
        bg.style.backgroundImage= 'url(background.jpg)';
    }
</script>
<header class="headBack">

</header>
<div class="slideButtons">

<ul>
    <li class="active">
        <button class="button" onclick="ps()"></button>
    </li>
</ul>

my css

.headBack {

background-image: url(background2.jpg);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: top center;
height: 600px;

}
1
  • bg.style.backgroundImage= "url('background.jpg')"; try this instead. Commented Jul 23, 2017 at 16:44

1 Answer 1

1

You need to use document.getElementsByClassName not document.getElementsByTagName for your requirement

 function ps() {
        var bg = document.getElementsByClassName('headBack')[0];
        bg.style.backgroundImage= 'url(background.jpg)';
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that makes sense I'll try as soon as I get home!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.