Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2008
    Posts
    36
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Javascript only changing background image of one element?

    Basically, I'm writing a script that changes the background images of my navigation set; and for some reason when clicking the specific link to make this happen - only the first 2 effects take place.

    Code:
    function changeTheme(theme)
    {
    	if (theme = "red")
    		{
    			var body = document.getElementsByTagName('body')[0];
    			body.style.backgroundColor = '#F78181';
    			document.getElementById("navlink").style.backgroundImage = "url('images/layout/red/navlinkbackground.png')";
    			document.getElementById("navlink1").style.backgroundImage = "url('images/layout/red/navlinkbackground.png')";
    			document.getElementById("navlink2").style.backgroundImage = "url('images/layout/red/navlinkbackground.png')";
    			document.getElementById("navlink3").style.backgroundImage = "url('images/layout/red/navlinkbackground.png')";
    			document.getElementById("navlink4").style.backgroundImage = "url('images/layout/red/navlinkbackground.png')";
    		}
    }
    So basically, the only thing that happens is the body background colour changes, and "navlink" only changes background image, whereas the others do not.

    Am I over looking something extremely simple or does Javascript just hate me?

    Thanks for any help given and it's greatly appreciated!

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,297
    Thanks
    23
    Thanked 612 Times in 611 Posts
    This works for me. Check the IDs of your objects in the HTML code. If this isn't it post all the code.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Code:
    if (theme = "red")
    will assign the string "red" to the variable theme

    most likely what you are looking for is
    Code:
    if (theme == "red")
    or (more strictly but somewhat pointless here)
    Code:
    if (theme === "red")
    which will check to see if the string "red" has already been assigned to the variable theme, and if so it will run the proceeding code


  •  

    Tags for this Thread

    Posting Permissions

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