0

I pretty much don't know jquery at all. Knowing HTML, PHP and CSS I can understand it a bit and alter already existing scripts but now I have hitted an brick wall.

I'm trying to create image gallery where you can click an image and it shows fullscreen as background. The problem I'm having is that I can't get the background to change.

What I tried was to change the background image of "background" by clicking "changer". The image that was supposed to be the new background is DSC_2363 which I put in the function.

EDIT: You can see the page in progress at http://lautamaki.net/omasivu2/

My CSS is like this:

      .background{
        background-image: url(images/DSC_1418.JPG);
        background-size: cover;
        background-color: black;
        width: 100%; height: 100%;
        position: fixed; top: 0; left: 0;
      }

Jquery:

    $('.changer').click(function(image){
      $('.background').css("background-image", "url(images/".image.".JPG)");
    });

And finally HTML:

<div class="background">
</div>
<div class="content">
  <img class="changer" src="images/DSC_2363.JPG" width="30" onClick="function(DSC_2363)">
</div>

I know there are already questions like these but none of those helped me at all. Hopefully someone gets what I'm trying and can help me, thanks!

EDIT: Changed

 $('.background').css("background-image", "url(images/".image.".JPG)");

to

 $('.background').css("background-image", "url(images/"+image+".JPG)");

but still not working.

1

3 Answers 3

1

You need to use + instead of . to concatenate string in Javascript

$('.changer').click(function(image){
    $('.background').css("background-image", "url(images/" + image + ".JPG)");
});
Sign up to request clarification or add additional context in comments.

Comments

0

try this:

 $('.changer').on('click',function(image){
      $('.background').css("background-image", "url(images/"+image+".JPG)");
    });

Comments

0

In JavaScript we use + for concatenation not .

$('.changer').click(function (image) {
    $('.background').css("background-image", "url(images/" + image + ".JPG)");
    //                                                     ^       ^
});


Updated code after OP Updated Question

Change your html add data-image = "DSC_2363" instead of onclick function

<div class="content">
  <img class="changer" data-image = "DSC_2363" src="images/DSC_2363.JPG" width="30" >
</div>

js

$('.changer').click(function () {
    var image = $(this).data('image'); //get data-image attribute
    $('.background').css("background-image", "url(images/" + image + ".JPG)");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.