Results 1 to 1 of 1

Thread: JS changing CSS background image

  1. #1
    Join Date
    Oct 2010
    Posts
    1

    JS changing CSS background image

    What I am trying to do is when a button is clicked in a link I want to run a function, which will change the background image of a div ID in an external CSS file in order to show the over state image.

    Before I have just used the css a:hover method but want to try a different way.

    So far I have this:

    HTML
    HTML Code:
    <a href="index.html" id="indexLink" onclick="indexNavBar()">Home</a>

    External CSS
    HTML Code:
    #navBar{
    		background-image:url(../html/images/navBar/navBar.jpg); <--- bit i want to change
    }
    External JS

    Done a bit more reading and and came up with this but it aint working.

    HTML Code:
    function indexNavBar(){
    	var imagePath = document.getElementById("navBar").style.backgroundImage;
    	
    	if (imagePath == "url(../html/images/navBar/navBar.jpg)")
    	{
    		document.getElementById("navBar").style.backgroundImage = "url(../html/images/navBar/homeOver.jpg)";
    	} else {
    			document.getElementById("navBar").style.backgroundImage = "url(../html/images/navBar/navBar.jpg)";
    		   }	
    }
    Last edited by sam150; 29 Oct 2010 at 12:23 PM.

Similar Threads

  1. css background image alignment only works for IE
    By pokinski in forum Web Design, HTML Reference and CSS
    Replies: 1
    Last Post: 06 Feb 2007, 08:24 AM
  2. how to set a background image with CSS
    By webguy_dave in forum Web Design, HTML Reference and CSS
    Replies: 1
    Last Post: 17 Dec 2005, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •