Jump to content

switch background image


turpentyne

Recommended Posts

I have a "read more" toggle button on my page that uses this basic script - I want it to also change the background image listed in the stylesheet, but it doesn't work. Any thoughts?

 

<script type="text/javascript">


function showme(id, linkid) {
        var divid = document.getElementById(id);
        var toggleLink = document.getElementById(linkid);
        if (divid.style.display == 'block') {
            toggleLink.innerHTML = '<img src="images/readmore.png">';
            divid.style.display = 'none';
		document.container_homepage.style.backgroundImage="url('http://www.workshopsaz.org/images/bgx.jpg')";

        }
        else {
            toggleLink.innerHTML = '<img src="images/readless.png">';
            divid.style.display = 'block';
		document.container_homepage.style.backgroundImage="url('http://www.workshopsaz.org/images/bgx2.jpg')";
        }
    }
</script>

Link to comment
Share on other sites

To make your script more lighweight, you can use the same element, but define two different css classes/ids, and switch those. For example:

 

HTML:

<div id="background-div" class="normal"></div>

 

CSS:

.normal
{
  background-image:url(/images/background-image1.jpg);
}

.readmore
{
  background-image:url(/images/background-image2.jpg);
}

 

Then you switch the class with javascript:

document.getElementById("some_anchor_link").onclick = function()
{
  document.getElementById("background-div").setAttribute("class", "readmore");
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.