Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1095429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:07:22+00:00 2026-05-17T00:07:22+00:00

I need to switch my element background image beetwin two images on every click

  • 0

I need to switch my element background image beetwin two images on every click on it.

I have this, byt it swithes color just two times =\

<html>
<head>
    <title>Javascript Change Div Background Image</title>

    <style type="text/css">

    #div1 {
    width:100px;
    height:30px;
    background-image:url(blue.png);
    }


    </style>

    <script language="javascript" type="text/javascript">
        function changeDivImage() {
            var imgPath = new String();
            imgPath = document.getElementById("div1").style.backgroundImage;

            if (imgPath == "url(blue.png)" || imgPath == "") {
                document.getElementById("div1").style.backgroundImage = "url(green.png)";
            }
            else {
                document.getElementById("div1").style.backgroundImage = "url(blue.png)";
            }
        }
    </script>

</head>

<body>

    <center>
    <p>
        This Javascript Example will change the background image of<br />
        HTML Div Tag onclick event.
    </p>
    <div id="div1">
    </div>
    <br />
    <input type="button" value="Change Background Image" onclick="changeDivImage()" />
    </center>

</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:07:23+00:00Added an answer on May 17, 2026 at 12:07 am

    Some browsers are likely giving you the absolute path to the image instead of the relative path you are providing.

    Instead of ==, test for the indexOf() position of the url.

    Example: http://jsfiddle.net/7Rp2G/2/ (click the button several times to see the url change)

    function changeDivImage() {
        var imgPath = new String();
        var div = document.getElementById("div1");
        imgPath = div.style.backgroundImage;
    
        div.style.backgroundImage = (imgPath.indexOf("blue.png") > -1 || imgPath == "") 
                                  ? "url(green.png)" 
                                  : "url(blue.png)";
    
    }​
    

    EDIT: Updated to replace the if() with a ternary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an element in a div, which has a background image. Below the
I have problem with XML schema. I need inside one element elements of three
I'm not sure what is happening. I have this code: `case (XMLREADER::ELEMENT): $node_type =
In my app, I want to have a single background image that I use
I have a Javascript form validator function something like switch (element.getAttribute('validationtype')) { case 'text':
I need to switch out an IP address in multiple WCF services in web.config.
For various reasons (code review mostly) I need to switch from current development branch
Currently I am working on selenium IDE, now I need to switch to selenium
We need to identify and then process switch/case statements in code based on some
NOTE: I am aware of the SVN relocate switch, I just need to know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.