Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css and/or javascript change background on clicking image

I have an issue where I have a div class="tasteTheRainbow" and inside are img tags. One tag in particular is a png class named .gA3.

Now tasteTheRainbow already has a css background url but when you tap or click the .gA3 I want the background url to change.

I have tried many other stackoverflow posts and found no solution.

Here is my HTML and my CSS:

<div class="tasteTheRainbow">
    <div class="greenArrow">
        <img class="gA1" src="assets/arrows/down.png"/>
        <img class="gA2" src="assets/arrows/in.png"/>
        <img class="gA3" src="assets/arrows/left.png"/>
        <img class="gA4" src="assets/arrows/out.png"/>
        <img class="gA5" src="assets/arrows/right.png"/>
        <img class="gA6" src="assets/arrows/up.png"/>
    </div>
</div>
.tasteTheRainbow {
    background-image: url(../assets/fivePix.png);
    background-repeat: repeat;
    position: absolute;
    background: url(../assets/tombrady1.jpg) no-repeat center center fixed !important;

    -webkit-background-size: cover;
    background-size: cover;
    display: block !important;
}

As I mentioned above I have tried using CSS classname:active. I have also tried multiple java script solutions and they simply do not change the background url to image1 to image2.

like image 793
RubberDucky4444 Avatar asked Apr 22 '15 03:04

RubberDucky4444


People also ask

How can change background of click in HTML?

Use HTML DOM Style backgroundColor Property to change the background color after clicking the button. This property is used to set the background-color of an element.

What is the code for changing the background in JavaScript?

background = color; } window. addEventListener("load",function() { changeBackground('red') }); Note: this does depend a bit on how your page is put together, for example if you're using a DIV container with a different background colour you will need to modify the background colour of that instead of the document body.

Can you set a background image in CSS?

The background-image CSS property sets one or more background images on an element.


1 Answers

My recommended solution is to create a CSS class for both the initial state of the element (example .tasteTheRainbow) as well as the new state (example .tasteTheRainbowNew), and then use Javascript to apply/remove the class.

$(function () { 
    $(".gA3").click(function () {
         $(this).parent(".tasteTheRainbow").toggleClass("tasteTheRainbowNew"); 
    }); 
});

See my JSfiddle here: http://jsfiddle.net/oxv0rc1k/2/

Also, note that I loaded in JQuery because my JS sucks without it.

like image 119
Brian John Avatar answered Oct 25 '22 17:10

Brian John