0

I tried the most voted answer on this thread but it isn't working for me.

Most voted answer said:

You probably want this (to make it like a normal CSS background-image declaration):

$('myOjbect').css('background-image', 'url(' + imageUrl + ')');

Here's the JS code:

$('.icon-menuoption').click(function() {

$('.menu').animate({
  left: "-285px"
}, 200);


$('body').animate({
  left: "0px"
}, 200);

$('jumbotron').css('background-image', 'url(' + /images/bg_v3.png + ')');

});

The menu and body move like they are supposed to but the jumbotron's background-image doesn't. I do have the bg_v3.png image in the images directory, which is in the same directory as this JS code.

Without this part:

$('jumbotron').css('background-image', 'url(' + /images/bg_v3.png + ')');

It does what it is supposed to but I also want to change the background-image.

Note: I am a complete noob. I am learning through codeacademy and have very little knowledge of JS, jQuery and CSS so please be as detailed as possible.

Thank you!

2
  • What does your markup look like? $('jumbotron') is not a right selector unless you actually have a tag <jumbotron> you probably want $('.jumbotron') Commented Jul 14, 2016 at 0:54
  • You can't do 'url(' + /images/bg_v3.png + ')' The JavaScript interpreter is going to try and parse /images/bg_v3.png as a variable. You should just be using 'url("/images/bg_v3.png")'. In the example you cited, imageUrl is a variable. Commented Jul 14, 2016 at 2:41

1 Answer 1

2

There are two things you need to correct in this line of code:

$('jumbotron').css('background-image', 'url(' + /images/bg_v3.png + ')');

First, the selector $('jumbotron') is not going to select any element. It returns null, hence calling the .css function will cause an error. If your targeting element has ID = 'jumbotron', then the selector should be changed to $('#jumbotron'). If that element has CSS class of .jumbotron, then the selector should be $('.jumbotron').

Secondly, your 'url(' + /images/bg_v3.png + ')' part also causes another error. This should be 'url(/images/bg_v3.png)'.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! In the end the only thing I had to change was to correct what I had to: $('.jumbotron') so this was one of those times you are programming, you scrtch your head for hours wondering what you are doing wrong and in the end all you had to do was add a dot. I think this is funny. Again, thanks a lot!

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.